修改
我不能group by
仅patient_id
,我会收到错误sql_mode=only_full_group_by...
我有以下查询帮助我获取所有被诊断患有糖尿病的患者名单:
SELECT t1.patient_id,
CONVERT(aes_decrypt(t4.patient_name_en, 'key1') USING utf8mb4) as patient_name_en,
t3.date_of_visit as date_of_visit,
t2.diagnosis_name,
max(ifnull(t5.date_of_assessment, 'N/A')) as date_of_assessment,
ifnull(t5.assessment_result, 0) as assessment_result
FROM consultation t1
LEFT JOIN diagnosis t2 ON t1.diagnosis_id = t2.diagnosis_id
LEFT JOIN visit t3 ON t3.visit_id = t1.visit_id
LEFT JOIN patient t4 ON t4.patient_id = t3.patient_id
LEFT JOIN diabetes_assessment t5 ON t5.patient_id = t4.patient_id
WHERE t2.diagnosis_name LIKE '%Diabetes%' AND t1.clinic_id = '361'
AND t3.visit_status="Active"
GROUP BY t1.patient_id, t5.date_of_assessment, t4.patient_name_en, t3.date_of_visit, t2.diagnosis_name, t5.assessment_result
ORDER BY t5.date_of_assessment DESC
结果是:
当您看到患者被多次诊断为不同类型或水平的糖尿病时,所有行都会显示出来。但我真正想要的是他第一次被诊断出来,所以结果只有这两行用蓝色突出显示:
此处患者0361首先在2017-04-06
中诊断出患有糖尿病。所以我们只为这名患者获得这一行。
我尝试使用`min(t3.date_of_visit)但它没有正常工作。
答案 0 :(得分:1)
我认为你想要的是第一次就诊和最后一次糖尿病评估。我假设你所有表中的第一个字段是auto_increment字段,并且小提琴中的consultation_id输入错误。
鉴于以上
MariaDB [sandbox]> select p.patient_name_en,v.*,c.diagnosis_id,d.diagnosis_name,da.date_of_assessment,da.assessment_result
-> from visit v
-> join patient p on p.patient_id = v.patient_id
-> join consultation c on c.patient_id = v.patient_id and c.visit_id = v.visit_id
-> join diagnosis d on d.diagnosis_id = c.diagnosis_id
-> left join
-> (
-> select da.patient_id, da.date_of_assessment,da.assessment_result
-> from diabetes_assessment da
-> where da.diabetes_assessment_id = (select max(da1.diabetes_assessment_id) from diabetes_assessment da1 where da1.patient_id = da.patient_id)
-> ) da on da.patient_id = v.patient_id
-> where v.visit_id = (select min(visit_id) from consultation c where c.patient_id = v.patient_id)
-> and c.diagnosis_id in (1,2)
-> and v.clinic_id = 361
-> ;
+-----------------+----------+------------+-----------+---------------+--------------+--------------+---------------------------------------------+--------------------+-------------------+
| patient_name_en | visit_id | patient_id | clinic_id | date_of_visit | visit_status | diagnosis_id | diagnosis_name | date_of_assessment | assessment_result |
+-----------------+----------+------------+-----------+---------------+--------------+--------------+---------------------------------------------+--------------------+-------------------+
| ABC | 1 | 361-9001 | 361 | 2017-03-03 | Active | 1 | Diabetes mellitus with diabetic nephropathy | 2017-05-05 | 40.00 |
| XYZ | 3 | 361-0361 | 361 | 2017-10-03 | Active | 2 | E01 Diabetes mellitus with kidney disease | 2017-03-10 | 30.50 |
+-----------------+----------+------------+-----------+---------------+--------------+--------------+---------------------------------------------+--------------------+-------------------+
2 rows in set (0.00 sec)
答案 1 :(得分:0)
只需保留组子句t1.patient_id, t5.date_of_assessment
。
所以我读了this doc 如果您不按订单要求按订单排序,请将其删除,或同时保留在组中。
答案 2 :(得分:0)
解决方案1: 使用Window Functions将数据库更改为某些内容,因为显然MySQL在此字段上有很大的缺点(for example)。这可以节省您的一天
解决方案2:由于第一种解决方案可能无法为您所接受,您可以尝试使用类似
的内容 select t1.patient_id, min(t3.date_of_visit) "date_of_visit"
from consultation t1
LEFT JOIN visit t3 ON t3.visit_id = t1.visit_id
group by t1.patient_id
您可以将其作为子查询加入或将其放入某个表(甚至可能是临时表)。加入应该由patient
和date_of_visit
完成。
不幸的是,直到你提供像草莓要求的MCVE一样,它将很难帮助更多。