我有一张桌子:
ID | Name | Date 1 | ABC | 2015-01-01 2 | XYZ | 2015-01-02 3 | ABC | 2015-01-03 4 | ABC | 2015-01-04
我想查询此表,结果如下:
ID | Name | Date | NextDate 1 | ABC | 2015-01-01 | 2015-01-03 2 | XYZ | 2015-01-02 | null 3 | ABC | 2015-01-03 | 2015-01-04 4 | ABC | 2015-01-04 | null
一个解决方案是:
select t1.*, (select min(t2.Date) from TAB t2 where t2.ID > t1.ID t2.Name = t1.Name) NextDate from TAB t1
但是这很慢,因为我们正在对每一行进行聚合.有没有比这更有效的替代解决方案?