我在运行查询时遇到问题。我试图加入3个表格,按照一年中规定的次数显示前10种药物。
当我按原样运行时,我收到有关聚合或选择中出错的错误消息。
这是我的疑问:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="main.js"></script>
我在联接中哪里出错以获得我想要显示的结果。
谢谢! - 安
答案 0 :(得分:1)
我相信你在寻找:
select Count(MED_ID), m.MEDICATION_NAME
from MEDICATIONS m Inner join
ENC_MEDICATIONS em
on em.MED_ID = m.MED_ID Inner JOIN
ENCOUNTER e
on em.ENC_ID = e.ENC_ID
where e.OBSDATE Between '2011-01-01' and '2011-12-31'
group by m.MEDICATION_NAME
order by COUNT(MED_ID) DESC
limit 10;
注意:
OBSDATE
在SELECT
中没有任何目的。答案 1 :(得分:0)
无可否认,您需要添加第二个非聚合组:
Select MEDICATIONS.MEDICATION_NAME, ENCOUNTER.OBSDATE, Count(enc_medications.MED_ID)
From MEDICATIONS
Inner JOIN ENC_MEDICATIONS On ENC_MEDICATIONS.MED_ID = MEDICATIONS.MED_ID
Inner JOIN ENCOUNTER On ENC_MEDICATIONS.ENC_ID = ENCOUNTER.ENC_ID
WHERE OBSDATE Between '01/01/2011' And '12/31/2011'
GROUP BY medications.MEDICATION_NAME, encounter.obsdate
ORDER BY COUNT(enc_medications.MED_ID) DESC;