0
我正在使用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功能,過去類似的東西。但是,從來沒有這樣複雜的查詢。我真的可以用一些幫助。
謝謝!
UPDATE:
在下面的意見,我已經更新了查詢到以下幾點:
;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)
不過,我仍然得到「Arithmic溢出錯誤轉換VARCHAR數據類型的數字「
任何額外的幫助,非常感謝。
這個查詢對我來說很有意義,而且看起來應該可以工作,但是我得到一個錯誤,指出「將varchar值6.6'轉換爲數據類型int」 – MLorenzen 2013-02-27 17:31:22
@MLorenzen時轉換失敗。 。 。我懷疑這是發生在查詢的其他地方。在某處,有一個從6.6到一個整數的隱式轉換。如果你的條件是'result_value <7'和'result_value'是一個varchar,就可能發生這種情況。在這種情況下,可以進行顯式轉換('cast(result_value as float)<7')或將常量改爲7.0。 – 2013-02-27 18:26:19
感謝您的跟進。我已經將.0添加到查詢中,但現在我得到了一個不同的錯誤。我已添加上面的更新查詢.. – MLorenzen 2013-02-27 20:54:17