SQL - 关于分组的建议

SQL - advice on grouping

SQL Server 2005。我不是在这里的编码答案之后(虽然它会很好)。我真的很想获得有关获得所需结果的最佳方法的建议。我对 pivot/unpivot/cte//rownumber 和动态查询有一些了解,但无法解决这个特定问题!数据示例如下。 注意:类型、位置、名称和描述的出现次数可以none到很多。

drop table #temp
create table #temp
(
event int,
type varchar(20),
locations varchar(20),
name varchar(30),
description varchar(50)
)
insert into #temp values (1,'support','r1','fred','desc 1')
insert into #temp values (1,'support','r1','fred','desc 2')
insert into #temp values (1,'support','r1','fred','desc 3')

insert into #temp values (1,'support','r1','jim','desc 1')
insert into #temp values (1,'support','r1','jim','desc 2')
insert into #temp values (1,'support','r1','jim','desc 3')

insert into #temp values (1,'support','r2','fred','desc 1')
insert into #temp values (1,'support','r2','fred','desc 2')
insert into #temp values (1,'support','r2','fred','desc 3')

insert into #temp values (1,'support','r2','jim','desc 1')
insert into #temp values (1,'support','r2','jim','desc 2')
insert into #temp values (1,'support','r2','jim','desc 3')

insert into #temp values (1,'work','r1','fred','desc 1')
insert into #temp values (1,'work','r1','fred','desc 2')
insert into #temp values (1,'work','r1','fred','desc 3')

insert into #temp values (1,'work','r1','jim','desc 1')
insert into #temp values (1,'work','r1','jim','desc 2')
insert into #temp values (1,'work','r1','jim','desc 3')

insert into #temp values (1,'work','r2','fred','desc 1')
insert into #temp values (1,'work','r2','fred','desc 2')
insert into #temp values (1,'work','r2','fred','desc 3')

insert into #temp values (1,'work','r2','jim','desc 1')
insert into #temp values (1,'work','r2','jim','desc 2')
insert into #temp values (1,'work','r2','jim','desc 3')

select * from #temp

我追求的结果是这样的..

1,support;work,r1;r2,fred;jim,desc1;desc2;desc3

这有点不相关,但是当像这样插入数据时,(对您而言)这样做会更容易(另外,尝试养成命名要插入的字段的习惯);

INSERT INTO #temp (event, type, locations, name, description)
VALUES (1,'support','r1','fred','desc 1')
,(1,'support','r1','fred','desc 2')
,(1,'support','r1','fred','desc 3')
,(1,'support','r1','jim','desc 1')
,(1,'support','r1','jim','desc 2')

您的目标似乎是 select 所有列的所有不同值,然后连接成一个字符串。而你只需要建议,所以我建议你去这里:multiple rows into a single row

看来您需要更多帮助:

select distinct
stuff((SELECT distinct'; ' + type-- as type
        FROM #temp 
        --order by type
        FOR XML PATH('')),1,1,'')
+ (SELECT distinct'; ' + locations
        FROM #temp 
        FOR XML PATH(''))
+ (SELECT distinct'; ' + name
        FROM #temp 
        FOR XML PATH(''))
+ (SELECT distinct'; ' + description 
        FROM #temp 
        FOR XML PATH(''))
 from #temp;

如果需要 4 列,则将 + (SELECT 更改为 , stuff((SELECT

查询就是这么简单:区分一列,更改为字符串,然后连接+(下一列)的字符串...

请不要给这个版本投票!所有赞成票都应该投给上面的解决方案!下面的代码只是为了完整性。这显示了根据@NayruLove

的回答将数据组织到单独列中的语法
SELECT  distinct x.event,
stuff((SELECT distinct'; ' + t.type 
     FROM #temp t 
    where t.event = x.event  
    FOR XML PATH('')),1,1,'') as type
, stuff((SELECT distinct'; ' + locations
    FROM #temp t
    where t.event= x.event
    FOR XML PATH('')),1,1,'') as room
, stuff((SELECT distinct'; ' + name
    FROM #temp t
    where t.event = x.event
    FOR XML PATH('')),1,1,'') as name
, stuff((SELECT distinct'; ' + description 
    FROM #temp t
    where t.event = x.event
    FOR XML PATH('')),1,1,'') as description
 from #temp x
group by x.event