繁体   English   中英

如果在T-SQL中的组中有多行,如何输出单列或连接另一列

[英]How to output a single column or concatenate another one if multiple rows are in group in T-SQL

想象一下以下结构:

Group   Color    ColorDesc
-----   -----    -----
1       'Red'    'The cool name of Red Color'
1       'Green'  'The cool name of Green Color'
2       'Blue'   'The cool name of Blue Color'
2       'Yellow' 'The cool name of Yellow Color'
2       'Purple' 'The cool name of Purple Color'
3       'Pink'   'The cool name of Pink Color'

我想对“ Group字段中的行进行Group ,如果Group中只有一行,我需要输出colorDesc列(至下面的Group = 3),但是如果有不止一列,我想获取字段颜色的分隔字符串(与1和2相同)。 所需的输出:

Group   GroupedColor
-----   -----
1       'Red', 'Green'
2       'Blue', 'Yellow', 'Purple'
3       'The cool name of Pink Color'

我可以创建一个多参数CLR聚合并愉快地运行,但是有没有一种有效的方法可以通过本机T-SQL实现呢?

这样尝试

SELECT DISTINCT Group,
    STUFF((
            SELECT ',' + Color
            FROM Table1 S
            WHERE T.Group = S.Group 
            FOR XML path('')
            ), 1, 1, '') [GroupedColor]
FROM Table1 T
Declare @t table(Groups int,Color varchar(50),ColorDesc varchar(50))
insert into @t
select 1 ,'Red',    'The cool name of Red Color' union all
select 1,'Green',  'The cool name of Green Color'  union all
select 2,'Blue',   'The cool name of Blue Color'  union all
select 2,'Yellow', 'The cool name of Yellow Color'  union all
select 2,'Purple', 'The cool name of Purple Color'  union all
select 3,'Pink',   'The cool name of Pink Color'

;with cte as
(
select groups,count(*) cnt from @t group by groups
)

select  distinct b.groups,case when cnt=1 then a.ColorDesc
else stuff((select ',' + color from @t c where c.groups=b.Groups for xml path('') ),1,1,'') end
from  cte b inner join @t a on a.Groups=b.Groups

Without distinct(Test both with lot of data)

Select * from 
(select  ROW_NUMBER() over(partition by b.groups order by b.groups) rn, b.groups,case when cnt=1 then a.ColorDesc
else stuff((select ',' + color from @t c where c.groups=b.Groups for xml path('') ),1,1,'') end colorDesc
from  cte b inner join @t a on a.Groups=b.Groups )t4 where rn=1

尝试这个:

SELECT DISTINCT T2.Group, 
       SUBSTRING((Select ','+
                      CASE WHEN (SELECT COUNT(Group) FROM T1 WHERE T2.Group=T1.Group)=1 
                      THEN T1.ColorDesc
                      ELSE T1.Color 
                      END AS [text()]
                  FROM Table1 T1
                  WHERE T1.Group = T2.Group
                  ORDER BY T1.Group
                  For XML PATH ('')),2, 1000) [Colors]
FROM Table1 T2

希望这可以提供答案,也可以带您接近答案。 尝试此操作,因为我没有时间尝试sqlFiddle。 不使用“组”作为列名,因此请考虑使用其他名称

SELECT DISTINCT Group1,
STUFF((
        SELECT ',' + 
        CASE WHEN (SELECT COUNT(Group1) FROM Table1  WHERE Group1=S.Group1)>1 
             THEN Color 
             ELSE ColorDesc 
        END
        FROM Table1  S
        WHERE T.Group1 = S.Group1
        FOR XML path('')
        ), 1, 1, '') [GroupColor]
FROM Table1  T

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM