我需要获得ColA,ColB,ColC的小组最低日期记录。如果日期相同,那么我需要得到最小的列E值。我的桌子很大。我不想使用子查询或加入。使用秩函数获得此任何其他方法?请adivce。
ColA,ColB,ColC,ColD,ColE
abc,12,1,2015-01-01 00:00:01,1234
abc,12,1,2015-01-01 00:00:01,1235
abc,12,2,2015-01-01 00:00:01,1236
xyz,13,1,2015-01-02 00:00:01,1237
xyz,13,1,2015-01-03 00:00:01,1238
def,12,1,2015-01-01 00:00:01,1239
abc,12,1,2015-01-03 00:00:01,1230
Output:
ColA,ColB,ColC,ColD,ColE
abc,12,1,2015-01-01 00:00:01,1234
abc,12,2,2015-01-01 00:00:01,1236
xyz,13,1,2015-01-02 00:00:01,1237
def,12,1,2015-01-01 00:00:01,1239
提前致谢。
答案 0 :(得分:0)
试试这个:
select ColA, ColB, ColC, min(ColD) as ColD,
case when min(ColD) = max(ColD) then max(ColE) else min(ColE) end as ColE
from TableName
group by ColA, ColB, ColC