Sql查询复制某些字段

时间:2018-01-22 07:42:07

标签: mysql sql

以下是我的SQL查询:

select given_name,value_numeric,name from 
(select given_name from person_name where person_id IN
(select patient_id from patient_identifier where identifier='BED203016'))
as alias,

(select value_numeric from obs where concept_id IN
(select concept_id from concept_name where name='MediObsForm_Quantity' and concept_name_type='FULLY_SPECIFIED' and locale='en')
 and person_id=86)
 as alias2,

 (select name from concept_name where concept_id IN
 (select value_coded from obs where concept_id IN
 (select concept_id from concept_name where name='MediObsForm_Drug_Name' and concept_name_type='FULLY_SPECIFIED' and locale='en')
 and person_id=86) 
 and locale='en')
 as alias3;  

这给出了输出:

given_name  value_numeric  name
Amit             18        Med1
Amit             28        Med1  
Amit             18        Med2  
Amit             28        Med2  

这不是我预期的输出。我期待以下输出:

given_name  value_numeric  name  
Amit             18        Med1  
Amit             28        Med2  

我做错了什么?

修改:https://imgur.com/a/Rr9mB

在obs表中,person_id指的是特定用户。这里86指的是'Amit'。
在同一个表中,concept_id = 3769指的是concept_name表中的MediObsForm_Drug_Name 在obs表中,concept_id 3769具有由图片中的列“K”定义的值 因此它的值为concept_id = 3768.So在concept_name表中,concept_id = 3768的名称为Almox dt ...即Med1

同样,obs中的concept_id = 3791指的是concept_name表中的MedObsForm_Quantity。在obs表中,列'O'表示value_numeric,它是数量。在pic中它是28和18。

1 个答案:

答案 0 :(得分:0)

select cn2.name as Medicine_Name,o.obs_group_id,o.person_id, data.Qty from obs o
inner join concept_name cn2 on o.value_coded=cn2.concept_id and o.concept_id=3769
and cn2.concept_name_type='FULLY_SPECIFIED' and cn2.locale ='en'

inner join (select o.value_numeric as Qty,o.obs_group_id from obs o
inner join concept_name cn2 on o.concept_id=cn2.concept_id and o.concept_id=3791
and cn2.concept_name_type='FULLY_SPECIFIED' and cn2.locale ='en'
) data on data.obs_group_id =o.obs_group_id;  

这是解决方案。