我目前有3张桌子(诊断,遭遇和遭遇诊断)。 1 is the link to see the table outputs
我目前编写的代码可以提供我想要的所有内容,但我正在寻找第一次诊断。 code and output
这是代码。谁能告诉我如何摆脱重复?
SELECT a.code, a.title, c.start_dts
FROM edw_emr_ods.diagnoses AS a
JOIN edw_emr_ods.encounter_diagnoses as b on a.diagnosis_id=b.diagnosis_id
JOIN edw_emr_ods.encounters as c on b.encounter_id=c.encounter_id
ORDER BY a.title ASC;
我尝试了分组,但是我收到了a.code
未在GROUP中列出的错误
对不起,我是SQL新手,试图通过测试,这是一个练习题
答案 0 :(得分:1)
我正在寻找第一次诊断。
在这种情况下,我认为如果您执行group by
并使用MIN
作为日期会有所帮助,或多或少那样
SELECT a.code, a.title, MIN(c.start_dts)
FROM edw_emr_ods.diagnoses AS a
JOIN edw_emr_ods.encounter_diagnoses AS b on a.diagnosis_id=b.diagnosis_id
JOIN edw_emr_ods.encounters AS c on b.encounter_id=c.encounter_id
GROUP BY a.code, a.title
ORDER BY a.title;
答案 1 :(得分:0)
你的小组应该是这样的:
GROUP BY a.code, a.title, c.start_dts
如果你想要单独的c.start_dts行,那就是这样。如果您只想要第一个start_dts,那么您的代码将是:
SELECT a.code, a.title, MIN(c.start_dts)
FROM edw_emr_ods.diagnoses AS a
JOIN edw_emr_ods.encounter_diagnoses as b on a.diagnosis_id=b.diagnosis_id
JOIN edw_emr_ods.encounters as c on b.encounter_id=c.encounter_id
GROUP BY a.code, a.title
ORDER BY a.title ASC;