我正在使用 SQL Server 2008R2 数据库。我有一个查询,用于从我们的数据库中获取一些实验室结果数据,这些数据是针对访问我们办公室超过 2 次的患者。这是查询:
;WITH PatientData (patient_id,last_name, first_name, ethnic_group,
race, icdcode, encounter_date, test_performed, result_value,
result_units )
AS
(
SELECT pat.patient_id, pat.last_name, Pat.first_name, pat.ethnic_group,
pat.race, pl.icdcode, enc.encounter_date, lab.test_performed, lab.result_value,
lab.result_units
FROM dbo.dem_patient pat
RIGHT JOIN dbo.med_problemlist as pl
ON pat.patient_id = pl.patient_id
JOIN dbo.enc_encounter as enc
ON pat.patient_id = enc.patient_id
JOIN dbo.med_labresult as lab
ON pat.patient_id = lab.patient_id
WHERE pl.icdcode like '250%' AND
enc.encounter_date >= '01/01/2012' AND enc.encounter_date <= '12/31/2012' AND
pat.last_name != 'test' AND (lab.test_performed = 'HgbA1c' OR
lab.test_performed = 'Hemoglobin A1c')
GROUP BY pat.patient_id, pat.last_name, Pat.first_name, pat.ethnic_group,
pat.race, pl.icdcode, enc.encounter_date, lab.test_performed, lab.result_value,
lab.result_units
)
SELECT patient_id,last_name, first_name, ethnic_group,
race, icdcode, encounter_date, test_performed, result_value,
result_units
FROM PatientData pd1
WHERE EXISTS (SELECT patient_id
FROM PatientData pd2
WHERE pd2.patient_id = pd1.patient_id
GROUP BY patient_id
HAVING COUNT(*)>1 )
ORDER BY pd1.race, pd1.ethnic_group
这适用于获取患者列表及其实验室结果值。它返回如下内容:
patient_id encounter_date test_performed result_value result_units
00001 01/15/2012 HgbA1c 5.6 %
00001 05/03/2012 HgbA1c 8.6 %
00025 02/02/2012 HgbA1c 9.1 %
00064 07/01/2012 HgbA1c 7.6 %
但是,我现在想对这些数据进行分组,并获得不同结果值之间的结果总数。例如,我希望得到如下所示的结果:
LessThan7% 7%To8% 8%To9% GreaterThan9%
775 289 365 154
过去我可以使用 CROSS JOIN 功能做类似的事情。但是,从来没有这样复杂的查询。我真的可以使用一些帮助。
谢谢!
更新:
根据以下建议,我已将查询更新为以下内容:
;WITH PatientData (patient_id,last_name, first_name, ethnic_group,
race, icdcode, encounter_date, test_performed, result_value,
result_units )
AS
(
SELECT pat.patient_id, pat.last_name, Pat.first_name, pat.ethnic_group,
pat.race, pl.icdcode, enc.encounter_date, lab.test_performed, lab.result_value,
lab.result_units
FROM dbo.dem_patient pat
RIGHT JOIN dbo.med_problemlist as pl
ON pat.patient_id = pl.patient_id
JOIN dbo.enc_encounter as enc
ON pat.patient_id = enc.patient_id
JOIN dbo.med_labresult as lab
ON pat.patient_id = lab.patient_id
WHERE pl.icdcode like '250%' AND
enc.encounter_date >= '01/01/2012' AND enc.encounter_date <= '12/31/2012' AND
pat.last_name != 'test' AND (lab.test_performed = 'HgbA1c' OR
lab.test_performed = 'Hemoglobin A1c')
GROUP BY pat.patient_id, pat.last_name, Pat.first_name, pat.ethnic_group,
pat.race, pl.icdcode, enc.encounter_date, lab.test_performed, lab.result_value,
lab.result_units
)
select sum(case when isnumeric(result_value) = 1
then (case when result_value < 7.0 then 1 else 0 end)
end) as [LessThan7%],
sum(case when isnumeric(result_value) = 1
then (case when result_value >= 7.0 and result_value < 8.0 then 1 else 0 end)
end) as [7%to8%],
sum(case when isnumeric(result_value) = 1
then (case when result_value >= 8.0 and result_value < 9.0 then 1 else 0 end)
end) as [8%to9%],
sum(case when isnumeric(result_value) = 1
then (case when result_value >= 9.0 then 1 else 0 end)
end) as [GreaterThan9%]
FROM PatientData pd1
WHERE EXISTS (SELECT patient_id
FROM PatientData pd2
WHERE pd2.patient_id = pd1.patient_id
GROUP BY patient_id
HAVING COUNT(*)>1 )
但是,我仍然收到“将 varchar 转换为数字数据类型的算术溢出错误”
非常感谢任何额外的帮助。