在肯尼亚基苏木的一家医院里,我有一个openmrs版本1.9.7的本地植入。由于OpenMrs数据库的复杂性,当我试图编写查询以访问从数据库收集的病人数据以进行数据管理时,就会出现问题。由于我已经离开了一段时间,所以我在sql方面也有点浮躁,但我需要尽快把数据拿出来。我当前的查询如下
SELECT p.date_created as date_enrolled, pi.identifier, pi.identifier_type identifier_type ,
pn.given_name,pn.middle_name, pn.family_name, p.person_id, p.gender, p.birthdate, p.death_date,
ob.obs_datetime, cm.name as obs_type, CASE co.datatype_id when '1' then ob.value_numeric
when '2' then (select name from concept_name where concept_id = ob.value_coded limit 1)
when '3' then ob.value_text when '6' then ob.value_datetime when '10' then ob.value_boolean when '13' then ob.value_complex else "N/A" END AS obs_value, e.encounter_datetime
FROM person p JOIN person_name pn ON p.person_id = pn.person_id
JOIN patient_identifier pi ON p.person_id = pi.patient_id
JOIN patient_identifier_type pit ON pit.patient_identifier_type_id = pi.identifier_type
JOIN obs ob ON p.person_id = ob.person_id JOIN encounter e ON e.encounter_id = ob.encounter_id
JOIN concept_name cm ON ob.concept_id = cm.concept_id
JOIN concept co ON ob.concept_id = co.concept_id
JOIN concept_datatype cdt ON cdt.concept_datatype_id = co.datatype_id;是否有一个已经存在的查询可以作为启动程序使用,或者可以修改以满足我的需要?或者更确切地说,你给我的建议是怎样才能让我经历这一切?
谢谢
发布于 2016-07-01 00:32:05
考虑到所有涉及到的表格,你做得相当不错。这是非常复杂的SQL,因为您将头数据和细节数据混合在一行中。如果您想在一个查询中编写一些东西,这里有两个相当大的问题: 1) concept_name有多个地区(语言),而我怀疑您只想要一个。尝试类似locale='en‘这样的方法来减少行数。2) obs表是EAV (实体-属性值),另外,正如您所发现的,不同的值字段对应于不同的数据类型。要处理这个问题,请看MySql的GROUP_CONCAT函数。这里是您的查询的一个粗略的尝试,虽然我删除了一些东西,以简化和仅仅是为了让它工作。它将让您了解如何使用GROUP_CONCAT,但我在数据类型->值CASE语句上作了介绍:
SELECT p.date_created as date_enrolled,
pi.identifier, pi.identifier_type, pn.given_name,
left(GROUP_CONCAT(ob.obs_datetime, cm.name SEPARATOR '\t'),60) as 'ItemDate|ItemName',
e.encounter_datetime
FROM person p JOIN person_name pn ON p.person_id = pn.person_id
JOIN patient_identifier pi ON p.person_id = pi.patient_id
JOIN patient_identifier_type pit ON pit.patient_identifier_type_id = pi.identifier_type
JOIN obs ob ON p.person_id = ob.person_id
JOIN encounter e ON e.encounter_id = ob.encounter_id
JOIN concept_name cm ON ob.concept_id = cm.concept_id
JOIN concept co ON ob.concept_id = co.concept_id
JOIN concept_datatype cdt ON cdt.concept_datatype_id = co.datatype_id
where cm.locale = 'en'
group by 2 limit 10;尽管如此,我不认为OpenMRS的人会建议你继续这样做。使用他们的报告工具。
https://stackoverflow.com/questions/35623907
复制相似问题