我对 SQL Server 2008 还很陌生,但遇到了问题。我有一个表,其中包含一个带有实验室结果的 varchar(255) 列。列中的数据如下所示:
result_value
7.0
162
NEGATIVE
SEE NOTE
9.6
现在,我要做的是运行以下查询:
;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 result_value < 7.0 then 1 else 0 end) as [LessThan7%],
sum(case when result_value >= 7.0 and result_value < 8.0 then 1 else 0 end) as [7%to8%],
sum(case when result_value >= 8.0 and result_value < 9.0 then 1 else 0 end) as [8%to9%],
sum(case when result_value >= 9.0 then 1 else 0 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 )
由于我应该只返回进行了 A1c 测试的患者,因此 results_value 列应该始终看起来像 xx - 但是,我尝试运行此查询时,我得到“将 varchar 转换为数据类型 numeric 的算术溢出错误。
虽然我意识到将这些不同类型的数据放在同一列中并不理想,但我并没有创建数据库——我只需要使用它。反正有这个问题吗?
谢谢您的帮助。