2015-11-04 39 views
0

我有一個表像的下面:SQL - 輸出列,如果另一列具有最大值

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。

回答

2

你可以做到這一點使用FIRST_VALUE()(見here):

select t.*, 
     first_value(Comments) over (partition by CustId order by Date) as newCol 
from table t; 
相關問題