获取表中数据组的记录总数

时间:2013-02-27 16:49:21

标签: sql sql-server-2008

我正在使用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转换为数据类型数字的算术溢出错误”

非常感谢任何其他帮助。

1 个答案:

答案 0 :(得分:1)

这是一个条件聚合:

with query as (your query here)
select sum(case when result_value < 0.07 then 1 else 0 end) as [LessThan7%],
       sum(case when result_value >= 0.07 and result_value < 0.08 then 1 else 0 end) as [7%to8%],
       sum(case when result_value >= 0.08 and result_value < 0.09 then 1 else 0 end) as [8%to9%],
       sum(case when result_value >= 0.09 then 1 else 0 end) as [GreaterThan9%]
from query

安排您的查询以进入with子句。

如果result_value存储为字符,请尝试以下操作:

select sum(case when isnumeric(result_value) = 1
                then (case when result_value < 0.07 then 1 else 0 end)
            end) as [LessThan7%],
       sum(case when isnumeric(result_value) = 1
                then result_value >= 0.07 and result_value < 0.08 then 1 else 0 end)
           end) as [7%to8%],
       sum(case when isnumeric(result_value) = 1
                then (case when result_value >= 0.08 and result_value < 0.09 then 1 else 0 end)
            end) as [8%to9%],
       sum(case when isnumeric(result_value) = 1
                then (case when result_value >= 0.09 then 1 else 0 end)
           end) as [GreaterThan9%]

您需要嵌套的case语句,以确保在{/ em>尝试转换之前isnumeric()运行