Поместите условие «Где» с несколькими столбцами содержимого в SQL Server без создания представления - PullRequest
0 голосов
/ 01 ноября 2019

Я написал запрос с несколькими столбцами содержимого, чтобы получить ожидаемый результат.

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';

Вот скриншот вывода, как и ожидалось. enter image description here

В приведенных выше результатах я хотел бы отфильтровать больше, добавив, где условие, как показано ниже.

где Type_of_Committee = 'Resources' и Committee_Reference_Number = 70?

Как изменить запрос для достижения ожидаемого?

1 Ответ

0 голосов
/ 01 ноября 2019

Достиг ожидаемого результата путем изменения запроса.

select w2.WF_ID,wm.Map_MapObjID, w2.WF_ValStr Type_of_Committee,stuff((select N' ; ' + cast (usid as nvarchar)
           from (select k.Name usid
                 from  w3,kuaf k 
                 where w3.WF_AttrID = 17 and w3.WF_ID=w2.WF_ID and k.ID=w3.WF_ValInt) AS t  
           for xml path('')), 1, 2, '') + N'' Secretariat_Attendees,
           stuff((select N' ; ' + cast (usid as nvarchar)
           from (select k.Name usid
                 from  w3,KUAF k  
                 where w3.WF_AttrID = 18 and w3.WF_ID=w2.WF_ID  and k.ID=w3.WF_ValInt) AS t  
           for xml path('')), 1, 2, '') + N'' Internal_Presenter_Attendees,

                   stuff((select N' ; ' + cast (usid as nvarchar)
           from (select k.Name usid
                 from  w3,KUAF k  
                 where w3.WF_AttrID = 5 and w3.WF_ID=w2.WF_ID  and k.ID=w3.WF_ValInt) AS t  
           for xml path('')), 1, 2, '') + N'' Committee_Attendees,


           stuff((select N' ; ' + + cast (usid as nvarchar)
           from (select w3.WF_ValStr usid 
                 from  w3
                 where w3.WF_AttrID = 8 and w3.WF_ID=w2.WF_ID ) AS t  
           for xml path('')), 1, 2, '') + N'' External_Presenter_Attendees,
           w3.WF_ValDate Action_Sheet_Date,
           w4.WF_ValDate Meeting_Date,
           w5.WF_ValStr Committee_Reference_Number,
           w6.WF_ValStr Subject,
           w7.WF_ValLong Details,
           w8.WF_ValLong Committee_View,
           w9.WF_ValLong Committee_Decision,
           w10.WF_ValLong Committee_Recommendation


           from  w2,  wm,  ws, w3, w4, w5, w6, w7, w8, w9, w10
where w2.WF_ID=ws.SubWork_WorkID and w2.WF_AttrID=2 and wm.Map_MapObjID=35269818 and ws.SubWork_MapID=wm.Map_MapID 
and w3.WF_ID=w2.WF_ID and w3.WF_AttrID=3
and w4.WF_ID=w2.WF_ID and w4.WF_AttrID=4
and w5.WF_ID=w2.WF_ID and w5.WF_AttrID=11
and w6.WF_ID=w2.WF_ID and w6.WF_AttrID=12
and w7.WF_ID=w2.WF_ID and w7.WF_AttrID=13
and w8.WF_ID=w2.WF_ID and w8.WF_AttrID=14
and w9.WF_ID=w2.WF_ID and w9.WF_AttrID=15
and w10.WF_ID=w2.WF_ID and w10.WF_AttrID=16
and w2.WF_ValStr='Resources'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...