我认为应该是一个简单的查询,但它可能不是。我需要在一个查询中做两件事(最好):
基本上,该表是索赔数据列表,按以下方式组织......
示例数据:
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')
预期结果:
我尝试过这个查询,但它并不起作用:
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
答案 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
分组才能解决像你这样的冲突想要然后生成该聚合的摘要