从下面的结果中,我想使用冒号分隔来结合基于committees_attendees
的{{1}}:
Work_WorkID
这是上面查询的结果:
select distinct
w.Work_WorkID, k.Name as "Committees_Attendees"
from
DTree d, WMap m, WSubWork s, WWork w, WFAttrData a, KUAF k
where
d.DataID = m.Map_MapObjID
and m.Map_MapID = s.SubWork_MapID
and s.SubWork_WorkID = w.Work_WorkID
and w.Work_WorkID = a.WF_ID
and d.DataID = 35269818
and a.WF_AttrID = 5
and k.ID = a.WF_ValInt
这是预期的输出:
Work_WorkID Committees_Attendees
--------------------------------
35273587 Deva
35273587 Eswar
我尝试了如下所示的XML路径,但无法获得两列所需的输出。
Work_WorkID Committees_Attendees
-------------------------------------
35273587 Deva ; Eswar
结果:
SELECT DISTINCT
e.results AS "Secretariat_Attendees"
FROM
(SELECT
REPLACE(STUFF(CAST((SELECT ' ; ' + CAST(c.Secretariat_Attendees AS VARCHAR(MAX)), c.WorkID
FROM
(SELECT DISTINCT
w.Work_WorkID AS WorkID,
k.Name AS Secretariat_Attendees
FROM
DTree d, WMap m, WSubWork s, WWork w, WFAttrData a, KUAF k
WHERE
d.DataID = m.Map_MapObjID
AND m.Map_MapID = s.SubWork_MapID
AND s.SubWork_WorkID = w.Work_WorkID
AND w.Work_WorkID = a.WF_ID
AND d.DataID = 35269818
AND a.WF_AttrID = 17
AND k.ID = a.WF_ValInt) c
ORDER BY
c.WorkID
FOR XML PATH(''), TYPE) AS VARCHAR(MAX)), 1, 2, ''), ' ', '') AS results
FROM
wfattrdata t) e;
答案 0 :(得分:1)
这里是如何concat
您的专栏。
select * into #res from (
select '35273587' as Work_WorkID ,'Deva' Committees_Attendees
union all
select '35273587' ,'Eswar'
)res
select distinct t2.Work_WorkID,
stuff((select N' ; ' + Committees_Attendees
from (select Work_WorkID, Committees_Attendees
from #res t1 where t1.Work_WorkID = t2.Work_WorkID) AS t
for xml path('')
), 1, 2, '') + N''
from #res t2;
drop table #res
应用于您的实际表,它更易于使用cte
with cte as (
select Distinct w.Work_WorkID,k.Name as "Committees_Attendees" from DTree d,WMap m,WSubWork s,WWork w,WFAttrData a,KUAF k
where d.DataID=m.Map_MapObjID and m.Map_MapID=s.SubWork_MapID and s.SubWork_WorkID=w.Work_WorkID
and w.Work_WorkID=a.WF_ID and d.DataID=35269818 and a.WF_AttrID=5 and k.ID=a.WF_ValInt
)
select distinct t2.Work_WorkID,
stuff((select N' ; ' + Committees_Attendees
from (select Work_WorkID, Committees_Attendees
from cte t1 where t1.Work_WorkID = t2.Work_WorkID) AS t
for xml path('')
), 1, 2, '') + N''
from cte t2;
答案 1 :(得分:-1)
如果您正在谈论Oracle SQL,则可以使用LISTAGG(列表聚合)。它是一个聚合函数,可以完成此操作(将列转换为字符分隔的字符串)。
但是,它具有最大接受值的预期限制;因此您可能无法连接一千个XD列。
针对您的查询列出汇总信息:
SELECT "Work_WorkID", LISTAGG("Committees_Attendees", ' ; ') WITHIN GROUP (ORDER BY "Committees_Attendees") "Committees_Attendees"
FROM
(SELECT DISTINCT W.WORK_WORKID "Work_WorkID", K.NAME AS "Committees_Attendees"
FROM DTREE D, WMAP M, WSUBWORK S, WWORK W, WFATTRDATA A, KUAF K
WHERE D.DATAID = M.MAP_MAPOBJID AND M.MAP_MAPID = S.SUBWORK_MAPID AND S.SUBWORK_WORKID = W.WORK_WORKID
AND W.WORK_WORKID = A.WF_ID AND D.DATAID = 35269818 AND A.WF_ATTRID = 5 AND K.ID = A.WF_VALINT)
-- GROUP BY "Work_WorkID"
如果MySQL需要它,则可能要寻找GROUP_CONCAT。
希望有帮助!