我有一个表medicalclaims
,其中包含几列感兴趣的列:patient_id
,vendor
,claim_id
,diagnosis_code
,procedure_code
,和state
。
简而言之,我想提取patient_id
的总数和claim_id
的总数,但我也想检查某些diagnosis_code
,{{1 }}和procedure_code
。例如,我希望我的输出列看起来像这样:
state
当前,我正在使用此查询来查询供应商,patient_id计数和Claim_id计数:
vender | count(distinct patient_id) | count (distinct claim_id) | procedure_code = E123 | state = CA
但是,我不确定如何建立其余的列来捕获特定的SELECT distinct vendor, count (distinct patient_id) AS Patients, count (distinct claim_id) AS Claims
FROM medicalclaims
GROUP BY 1
ORDER BY 1
,diagnosis_code
和procedure_code
感兴趣的值。我想提取CA或NY患者的总数以及特定的诊断代码或程序代码的总和。因此,基本上在输出中添加如下所示的列:
state
和
SELECT count(distinct patient_id)
FROM medicalclaims
WHERE state = 'CA'
答案 0 :(得分:1)
我想你想要
SELECT vendor, count(distinct patient_id) AS Patients,
count(distinct claim_id) AS Claims,
sum(case when procedure_code = 'xyz' then 1 else 0 end) as procedure_xyz,
sum(case when state = 'xyz' then 1 else 0 end) as state_ca
FROM medicalclaims
GROUP BY 1
ORDER BY 1