Я написал запрос с несколькими столбцами содержимого, чтобы получить ожидаемый результат.
with cte as
(
select distinct
w.Work_WorkID, k.Name as "Attendees", a.WF_AttrID,a.WF_ValStr,a.WF_ValDate,a.WF_ValLong
from
d
INNER JOIN m ON d.DataID = m.Map_MapObjID
INNER JOIN s ON m.Map_MapID = s.SubWork_MapID
INNER JOIN w ON s.SubWork_WorkID = w.Work_WorkID
INNER JOIN a ON w.Work_WorkID = a.WF_ID
LEFT OUTER JOIN k ON k.ID = a.WF_ValInt
where
d.DataID = 35269818
and a.WF_AttrID IN( 5,17,18,8,2,3,4,11,12,13,14,15,16)
)
select distinct
t2.Work_WorkID,
stuff((select N' ; ' + Secretariat_Attendees
from (select Work_WorkID, t1.Attendees AS Secretariat_Attendees
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 17) AS t
for xml path('')), 1, 2, '') + N'' Secretariat_Attendees,
stuff((select N' ; ' + Internal_Presenter_Attendees
from (select Work_WorkID, t1.Attendees AS Internal_Presenter_Attendees
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 18) AS t
for xml path('')), 1, 2, '') + N'' Internal_Presenter_Attendees,
stuff((select N' ; ' + External_Presenter_Attendees
from (select Work_WorkID, t1.WF_ValStr AS External_Presenter_Attendees
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 8) AS t
for xml path('')), 1, 2, '') + N'' External_Presenter_Attendees,
(select t1.WF_ValStr AS Type_of_Committee
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 2 ) Type_of_Committee,
(select t1.WF_ValDate AS Action_Sheet_Date
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 3) Action_Sheet_Date,
(select t1.WF_ValDate AS Meeting_Date
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 4) Meeting_Date,
(select t1.WF_ValStr AS Committee_Reference_Number
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 11) Committee_Reference_Number,
(select t1.WF_ValStr AS Subject
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 12) Subject,
(select t1.WF_ValLong AS Details
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 13) Details,
(select t1.WF_ValLong AS Committee_View
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 14) Committee_View,
(select t1.WF_ValLong AS Committee_Decision
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 15) Committee_Decision,
(select t1.WF_ValLong AS Committee_Recommendation
from cte t1
where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 16) Committee_Recommendation,
stuff((select N' ; ' + Committee_Attendees
from (select Work_WorkID, t1.Attendees AS Committee_Attendees
from cte t1 where t1.Work_WorkID = t2.Work_WorkID AND t1.WF_AttrID = 5) AS t
for xml path('')
), 1, 2, '') + N'' Committee_Attendees
from cte t2 where t2.WF_ValStr='Resources';
Вот скриншот вывода, как и ожидалось.
В приведенных выше результатах я хотел бы отфильтровать больше, добавив, где условие, как показано ниже.
где Type_of_Committee = 'Resources' и Committee_Reference_Number = 70?
Как изменить запрос для достижения ожидаемого?