将数据存储在表中,并尝试将其查询出来,以便读取数据,例如 Motrin |毫克 | 25 | 25 4 |日 |疼痛,排成一排。从图像中...我将它们全部放在“答案”栏下。我的查询将它们全部显示出来......但它重复了数据。任何人都可以帮忙将其分组到 DrilldownQuestionID 下吗?
以下是我创建的示例数据:
练习ID | 患者ID | 父页面ID | 父页码 | 问题ID | 深入分析问题ID | 深入分析AnsRecordID | 遇到ID | 回答 |
---|---|---|---|---|---|---|---|---|
1 | 111111 | 48 | 5 | 1475 | 2091 | 1 | 遭遇1 | 泰诺 |
1 | 111111 | 48 | 5 | 1475 | 2092 | 1 | 遭遇1 | 毫克 |
1 | 111111 | 48 | 5 | 1475 | 2093 | 1 | 遭遇1 | 500 |
1 | 111111 | 48 | 5 | 1475 | 2094 | 1 | 遭遇1 | 3 |
1 | 111111 | 48 | 5 | 1475 | 2095 | 1 | 遭遇1 | 日 |
1 | 111111 | 48 | 5 | 1475 | 2096 | 1 | 遭遇1 | 头痛 |
1 | 111111 | 48 | 5 | 1475 | 2091 | 2 | 遭遇1 | 美林 |
1 | 111111 | 48 | 5 | 1475 | 2092 | 2 | 遭遇1 | 毫克 |
1 | 111111 | 48 | 5 | 1475 | 2093 | 2 | 遭遇1 | 25 |
1 | 111111 | 48 | 5 | 1475 | 2094 | 2 | 遭遇1 | 4 |
1 | 111111 | 48 | 5 | 1475 | 2095 | 2 | 遭遇1 | 日 |
1 | 111111 | 48 | 5 | 1475 | 2096 | 2 | 遭遇1 | 疼痛 |
下面是我的查询;
select distinct j.Answer as Name,
n.Answer as Dosage,
k.Answer as [Dosage Unit],
o.Answer as [How Many],
l.Answer as [How Often],
m.Answer as [Taken For]
from
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2091
where a.EncounterID = @EncounterID) as j
join
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2093
where a.EncounterID = @EncounterID) as n
on j.EncounterID = n.EncounterID
join
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2092
where a.EncounterID = @EncounterID) as k
on n.encounterID = k.EncounterID
join
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2094
where a.EncounterID = @EncounterID) as o
on k.EncounterID = o.EncounterID
join
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2095
where a.EncounterID = @EncounterID) as l
on o.encounterID = l.EncounterID
join
(select isnull(a.[Answer],'') as Answer,
a.EncounterID
from C_ItemDrillDown c join D_AWVAnswers a
on c.ItemDrilldownID = a.DrilldownQuestionID
and a.QuestionID = 1475
and a.DrilldownQuestionID = 2096
where a.EncounterID = @EncounterID) as m
on l.encounterID = m.EncounterID
我想要的结果是:
姓名 | 用量 | 剂量单位 | 有多少 | 多久一次 | 所为 |
---|---|---|---|---|---|
泰诺 | 500 | 毫克 | 3 | 日 | 头痛 |
美林 | 25 | 毫克 | 4 | 日 | 疼痛 |
所以看起来你正在追求直接的枢轴,如下所示:
select
max(case when DrilldownQuestionID = 2091 then Answer end) [Name],
max(case when DrilldownQuestionID = 2092 then Answer end) [Dosage Unit],
max(case when DrilldownQuestionID = 2093 then Answer end) Dosage,
max(case when DrilldownQuestionID = 2094 then Answer end) [How Many],
max(case when DrilldownQuestionID = 2095 then Answer end) [How Often],
max(case when DrilldownQuestionID = 2096 then Answer end) [Taken For]
from t
group by DrilldownAnsRecordID;
我省略了多余的(问题)列,如果您需要使用它们,您也可以对它们进行分组。
结果:
参见演示小提琴