查找组中的特定列是否全部为NULL,然后相应地填充目标。我有如下记录。我的要求是根据-填充输出列“总计” 1)如果所有行中的“ trans_dt”列均为NULL,则在组(或分区)中,然后在输出中填充“ total”列为zer0 2)如果任何记录具有有效值(在trans_dt列中),则使用该组的“项”最大值填充“总计”,并使用该组的max_dt作为该组的最大trans_dt
custid|transact_dt|items
------------------------
1234|05/01/2019|3
1234|10/02/2019|4
1234|Null|3
5678|Null|5
5678|Null|3
5678|Null|1
5678|Null|2
在上面的记录集中,“ 1234”的有效值在trans_dt中有2行,因此输出列“ total”应填充为“ 4”。但是,对于“ 5678”临时客户,所有trans_dt值均为Null,因此“ total”应填充为0。
custid|transact_dt|items
------------------------
1234|10/02/2019|4
5678|31/12/9999|0
select custid, max_trans_dt,
CASE WHEN max_trans_dt IS NULL then 0
ELSE total
END as total
from
( select custid, MAX(trans_dt) OVER (PARTITION BY custid) as max_trans_dt, MAX(items) OVER (PARTITION BY custid) as total,
ROW_NUMBER() OVER (PARTITION BY custid order by trans_dt desc, items desc) as rn ) tmp
WHERE tmp.rn = 1
是否有一种更智能,更清洁的解决方案来满足上述要求? 谢谢
答案 0 :(得分:0)
只需使用条件聚合:
dict-set!: contract violation
expected: (dict-implements/c dict-set!)
given: '#()
in: the d argument of
(->i
((d (dict-implements/c dict-set!))
(k (d) (dict-key-contract d))
(value (d) (dict-value-contract d)))
(_r void?))
contract from: <collects>/racket/dict.rkt