【问题标题】:SQL - Output a column if another column has max valueSQL - 如果另一列具有最大值,则输出一列
【发布时间】:2015-11-05 07:11:49
【问题描述】:

我有一张如下表:

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。

【问题讨论】:

    标签: sql teradata


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2011-09-20
      • 1970-01-01
      • 2021-04-21
      • 2021-04-27
      相关资源
      最近更新 更多