【问题标题】:Fetch valid value for a field when one type has two groups with null value and valid value当一种类型具有空值和有效值的两组时,获取字段的有效值
【发布时间】:2021-06-02 14:51:27
【问题描述】:

我有一张如下表:

ID  Amt type            accnum  rcnum   amt
1   Recovery type 1                 [NULL]  45
1   Total recovery                  [NULL]  34
1   Current Type 1      2345        123     23
1   Total current type              [NULL]  12
1   Current type 1                  [NULL]  78
1   Current rvrsl                   [NULL]  -908

现在我想创建如下,它总结了每种类型的 amt,但如果存在则填充有效值,否则将填充 NULL/空白:

ID  Amt type            accnum  rcnum   amt
1   Recovery type 1                 [NULL]  45
1   Total recovery                  [NULL]  34
1   Current Type 1      2345        123     101
1   Total current type              [NULL]  12
1   Current rvrsl                   [NULL]  -908

我正在尝试进行左连接,但我不确定如何在不删除其他记录的情况下实现此目的。

【问题讨论】:

    标签: sql group-by amazon-redshift


    【解决方案1】:

    这看起来像聚合:

    select id, amt_type, accnum, max(rcnum), sum(amt)
    from t
    group by id, amt_type, accnum;
    

    聚合函数忽略 NULL 值。

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2021-12-17
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      相关资源
      最近更新 更多