我对 SQL Server 还很陌生……但我有一个问题。我编写了以下查询,它可以很好地显示 2012 年患者的所有访问(遭遇)和实验室结果。
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 '401.9%' 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
ORDER BY race, ethnic_group
上述查询的结果如下所示:
patient_id last_name first_name ethnic_group race icdcode encounter_date lab_result
0 smith john HIS WHI 401.90 01/01/2012 9.1
0 smith john HIS WHI 401.90 10/12/2012 9.2
0 smith john HIS WHI 401.90 11/15/2012 9.7
5 doe jane NSP BLK 401.90 01/20/2012 11
6 davids david NSP BLK 401.90 04/25/2012 5.9
6 davids david NSP BLK 401.90 05/04/2012 6.4
但是,我现在只需要返回那些在 2012 年 1 月 1 日至 2012 年 12 月 31 日期间访问(遭遇)超过一次的患者。它看起来像这样(从上面的结果)
patient_id last_name first_name ethnic_group race icdcode encounter_date lab_result
0 smith john HIS WHI 401.90 01/01/2012 9.1
6 davids david NSP BLK 401.90 04/25/2012 5.9
谁能帮我这个?
更新:因此,使用下面的建议,我已将 HAVING COUNT(*) > 1 添加到查询中。但是,这给了我一些我不太明白的结果。
没有 HAVING COUNT(*) > 1 语句的查询结果:
1001131 BLA 401.9 48:48.3 Hemoglobin A1c 9.9
1001131 BLA 401.9 00:00.0 Hemoglobin A1c 9.9
1001131 BLA 401.9 44:44.3 Hemoglobin A1c 9.9
1001648 BLA 401.9 50:15.6 Hemoglobin A1c 6.5
1001648 BLA 401.9 16:15.1 Hemoglobin A1c 6.5
1001648 BLA 401.9 28:04.5 Hemoglobin A1c 6.5
1001648 BLA 401.9 17:00.2 Hemoglobin A1c 6.5
1002648 BLA 401.9 17:47.8 Hemoglobin A1c 6.1
1002648 BLA 401.9 52:38.0 Hemoglobin A1c 6.1
1002648 BLA 401.9 43:47.3 Hemoglobin A1c 6.1
1002648 BLA 401.9 02:55.9 Hemoglobin A1c 6.1
1002715 BLA 401.9 37:52.4 Hemoglobin A1c 13.1
1002715 BLA 401.9 44:41.0 Hemoglobin A1c 12.6
1002715 BLA 401.9 44:41.0 Hemoglobin A1c 13.1
1002715 BLA 401.9 37:52.4 Hemoglobin A1c 10.7
1002715 BLA 401.9 37:52.4 Hemoglobin A1c 12.6
1002715 BLA 401.9 00:57.9 Hemoglobin A1c 10.7
1002715 BLA 401.9 00:57.9 Hemoglobin A1c 12.6
1002715 BLA 401.9 00:57.9 Hemoglobin A1c 13.1
1002715 BLA 401.9 38:17.6 Hemoglobin A1c 10.7
1002715 BLA 401.9 38:17.6 Hemoglobin A1c 12.6
1002715 BLA 401.9 38:17.6 Hemoglobin A1c 13.1
1002715 BLA 401.9 44:41.0 Hemoglobin A1c 10.7
1002893 BLA 401.9 33:45.4 Hemoglobin A1c 5.9
1002893 BLA 401.9 22:58.5 Hemoglobin A1c 5.9
1002893 BLA 401.9 00:00.0 Hemoglobin A1c 5.9
1002893 BLA 401.9 55:11.3 Hemoglobin A1c 5.9
1003195 BLA 401.9 34:08.7 Hemoglobin A1c 6.4
1003195 BLA 401.9 50:03.6 Hemoglobin A1c 6.4
1003195 BLA 401.9 08:36.8 Hemoglobin A1c 6.4
1003195 BLA 401.9 33:56.6 Hemoglobin A1c 6.4
1003195 BLA 401.9 34:04.3 Hemoglobin A1c 6.4
使用 HAVING 语句查询的结果:
1001937 BLA 401.9 11/14/12Hemoglobin A1c 6.3
1001937 BLA 401.9 8/14/12 Hemoglobin A1c 6.3
1001937 BLA 401.9 5/14/12 Hemoglobin A1c 6.3
1001937 BLA 401.9 2/14/12 Hemoglobin A1c 6.3
1001937 BLA 401.9 11/14/12Hemoglobin A1c 6.3
1002034 BLA 401.9 1/10/12 HEMOGLOBIN A1c 7.7
1002034 BLA 401.9 6/8/12 HEMOGLOBIN A1c 7.7
1002034 BLA 401.9 9/7/12 HEMOGLOBIN A1c 7.7
1002034 BLA 401.9 10/15/12HEMOGLOBIN A1c 7.7
1002648 BLA 401.9 2/22/12 Hemoglobin A1c 6.1
1002648 BLA 401.9 7/2/12 Hemoglobin A1c 6.1
1002648 BLA 401.9 10/2/12 Hemoglobin A1c 6.1
1002648 BLA 401.9 2/22/12 Hemoglobin A1c 6.1
1020923 BLA 401.9 1/30/12 HEMOGLOBIN A1c 10
1020923 BLA 401.9 1/30/12 Hemoglobin A1c 11.8
1020923 BLA 401.9 4/17/12 Hemoglobin A1c 11.8
1020923 BLA 401.9 5/16/12 HEMOGLOBIN A1c 10
1020923 BLA 401.9 5/16/12 Hemoglobin A1c 11.8
1020923 BLA 401.9 9/13/12 HEMOGLOBIN A1c 10
1020923 BLA 401.9 9/13/12 Hemoglobin A1c 9.3
1020923 BLA 401.9 10/1/12 HEMOGLOBIN A1c 10
1020923 BLA 401.9 10/1/12 Hemoglobin A1c 11.8