SQL - 如果另一列具有最大值,则输出列

时间:2015-11-04 23:09:20

标签: sql teradata

我有一张如下表:

CustID   Date         Comments
A        JAN 1        abc    
A        JAN 5        def    
B        JAN 2        ttt    
B        JAN 7        hhh    
B        JAN 10       hhh    

我需要在此表中添加一列,以显示每个客户的最小日期行的“注释”列。下面的示例输出。

CustID   Date         Comments  NewCol
A        JAN 1        abc       abc    
A        JAN 5        def       abc 
B        JAN 9        ttt       hhh   
B        JAN 7        hgg       hhh    
B        JAN 3        hhh       hhh

我正在使用Teradata。

1 个答案:

答案 0 :(得分:2)

您可以使用FIRST_VALUE()执行此操作(请参阅here):

select t.*,
       first_value(Comments) over (partition by CustId order by Date) as newCol
from table t;
相关问题