如何计算表中的空值以及非空列的不同值

时间:2017-10-31 16:45:11

标签: null amazon-redshift distinct-values

我认为应该是一个简单的查询,但它可能不是。我需要在一个查询中做两件事(最好):

  1. 计算值为NULL的#of recs(在多列中)
  2. 计算特定列上的#of distinct recs
  3. 基本上,该表是索赔数据列表,按以下方式组织......

    1. 索赔号可以多次出现。我想计算不同的索赔号(此字段永远不会为NULL)
    2. NULL值可以出现在一列或多列中
    3. 示例数据:

      insert into t1 (ID, LOB, Funding, Claim_ID, Claim_Type, Pharmacy_ID)
      values (3617623, 'DRUG', NULL, 2389753478, 'ORG', 'OA734'),
      (3462090, 'DRUG', NULL, 2389753478, 'REV', 'OA734'), 
      (3587262, NULL, NULL, 5356201834, 'ORG', NULL), 
      (3160932, 'DRUG', NULL, 4627282840, 'ORG', NULL), 
      (3986523, 'DRUG', NULL, 4627282840, 'REV', NULL), 
      (3874627, 'DRUG', NULL, 7735624780, 'ORG', '43857')
      

      预期结果:

      1. 总记录数= 6
      2. Claim_ID = 4
      3. 的计数
      4. NULL LOB = 1
      5. 的计数
      6. NULL资金计数= 4
      7. NULL Claim_Type = 0
      8. 的计数
      9. 计数NULL Pharmacy_ID = 2
      10. 我尝试过这个查询,但它并不起作用:

        select
        sum (case when LOB is null then 1 else 0 end) as LOB_null,
        sum (case when Funding is null then 1 else 0 end) as Funding_null,
        sum (case when Claim_Type is null then 1 else 0 end) as Claim_Type_null,
        sum (case when Pharmacy_ID is null then 1 else 0 end) as Pharmacy_ID_null,
        sum (count (distinct (case when claim_id is not null then 1 end)) as ttl_claims,
        sum (case when ID is not null then 1 end) as ttl_recs
        from t1
        

1 个答案:

答案 0 :(得分:0)

您需要为满足指定条件的行计算不同的claim_id,而不是计算记录:

select
sum (case when ID is not null then 1 end) as ttl_recs,
count (distinct case when claim_id is not null then claim_id end) as ttl_claims,
count (distinct case when LOB is null then claim_id end) as LOB_null,
count (distinct case when Funding is null then claim_id end) as Funding_null,
count (distinct case when Claim_Type is null then claim_id end) as Claim_Type_null,
count (distinct case when Pharmacy_ID is null then claim_id end) as Pharmacy_ID_null
from t1

如果有任何更改,同一个claim_id可以在一行中有2行具有NULL属性而在另一行中有NOT NULL,则必须先按claim_id分组才能解决像你这样的冲突想要然后生成该聚合的摘要