【发布时间】:2017-11-01 07:39:25
【问题描述】:
我有我认为应该是一个简单的查询,但它可能毕竟不是。我需要在一个查询中做两件事(最好):
- 计算值为 NULL 的 #of recs(在多列中)
- 计算特定列上的不同记录数
基本上,该表是索赔数据的列表,并按以下方式组织...
- 一个索赔编号可以出现多次。我想计算不同的索赔编号(此字段永远不会为 NULL)
- NULL 值可以出现在一个或多个列中
样本数据:
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')
预期结果:
- 总记录数 = 6
- Claim_ID 计数 = 4
- NULL LOB 计数 = 1
- NULL Funding 计数 = 4
- NULL Claim_Type 的计数 = 0
- NULL Pharmacy_ID = 2 的计数
我尝试了这个查询,但效果不佳:
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
【问题讨论】:
标签: null amazon-redshift distinct-values