我怎样才能得到相同的component_id和值,而不是组合在一起

问题描述 投票:-2回答:1

这就是我想要做的事情,我希望使用相同的组件ID进行分组,但可以应用group by,因为这是视图中的列。

SELECT Business_Analyst_Team = STUFF((
                                      SELECT DISTINCT ', ' + A.ResourceName
                                        FROM  (
                                                Select ResourceName 
                                                      ,Component_ID
                                                 FROM [WFS].[Component_Resource] 
                                                 join  dbo.MSP_EPMResource_UserView 
                                                 on dbo.MSP_EPMResource_UserView.ResourceEmailAddress = [WFS].[Component_Resource].User_Email
                                              where [Role] ='Team Member' 
                                                and Functional_Group ='Product') A
                                               where A.Component_ID = [WFS].[Component_Resource].Component_ID
                                                 FOR XML PATH('')), 1, 2, '')
                                                FROM [WFS].[Component_Resource]

这就是我得到的,我希望具有相同component_id的名称应该位于同一列Business_Analyst_Team ** ** Component_ID

A                             1
b                           28439
c                           28439
d                           28439
e                           28439

想要这样

** Business_Analyst_Team ** 一个 B,C,d,电子

sql sql-server
1个回答
0
投票

使用子查询

select distinct * from 
(SELECT


    stuff(
    (
    select cast(',' as varchar(max)) + U.team
    from t U
    WHERE U.id = G.id
    order by U.team
    for xml path('')
    ), 1, 1, '') AS concat
FROM
    t G

    ) as a

http://sqlfiddle.com/#!18/d17b4/4

concat
A
b,c,d,e
© www.soinside.com 2019 - 2024. All rights reserved.