我对SQL服务器还不熟悉......但我遇到了问题。我写了下面的查询,它可以很好地显示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(*)&gt; 1到查询。但是,这给了我一些我不太了解的结果。
没有HAVING COUNT(*)&gt;的查询结果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
答案 0 :(得分:1)
从选择字段中删除日期,然后使用
Select FIELDS where condition group by FIELDS having count(*) > 1
答案 1 :(得分:0)
您的查询可能如下所示:
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
HAVING COUNT(*)>1
答案 2 :(得分:0)
离开我的头顶,将另一个连接条件添加到内联视图,在该视图中,您可以从遭遇次数为&gt;的集合中抓取最小遭遇日期。 1.连接条件是enc.encounterdate = inlineview.encounterdate。
您发布的示例结果看起来像寻找第一次遇到,所以请使用min,如果您想要上次遇到,请使用max。
答案 3 :(得分:0)
在您的结果中,lab_result和encounter_date具有不同的值。请求返回用于对数据进行分组的最小值
SELECT pat.patient_id, pat.last_name, Pat.first_name, pat.ethnic_group,
pat.race, pl.icdcode, MIN(enc.encounter_date), MIN(lab.result_value)
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
HAVING COUNT(*) > 1
ORDER BY race, ethnic_group
答案 4 :(得分:0)
;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 '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
)
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 p2.patient_id = pd1.patient_id
GROUP BY patient_id
HAVING COUNT(*)>1 )
ORDER BY pd1.race, pd1.ethnic_group
这样做是创建一个名为公用表表达式(CTE)的临时命名结果集。这包含所有数据。然后,选择只挑选出CTE中不止一次存在的那些记录。