【问题标题】:Compare the column values where there are null and not null values比较有空值和非空值的列值
【发布时间】:2017-06-22 02:58:43
【问题描述】:

我想比较表中的值,其中同一 ID 有空值和非空值(如果不是空值,那么我需要最少两个非空值)。如果给定 ID 的所有值都为空,我希望该 ID 的值显示为空。 输入

ID     Amount                           
1        Null 
1        Null                            
1        Null                            
1        500                              
1        600 
1        700
2        Null                             
2        Null 
2        Null
2        Null 
2        Null 
3        Null
3        Null
3        300
3        600
3        200

预期输出

ID          Amount
 1           500 (min Not null value)
 2           Null 
 3           200  (Min Not null value)

【问题讨论】:

    标签: sql tsql sql-server-2014


    【解决方案1】:

    简单的分组就可以解决问题:

    select t.id, min(t.amount)
    from table t
    group by t.id
    

    【讨论】:

    • 谢谢。真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2014-12-11
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多