SQL - 有关分组的建议

时间:2016-04-19 14:42:57

标签: sql sql-server-2005

SQL Server 2005.在此处编码回答之后我(尽管它会很好)。我真的在寻求最佳方法的建议,以获得我需要的结果。我对pivot / unpivot / cte // rownumber和动态查询有一些了解,但无法理解这个特定问题!下面是数据的一个例子。 注意:类型,位置,名称和描述的出现可以是无。

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您的目标似乎是选择所有列的所有不同值,然后连接成一个字符串。而你只需要建议,所以我建议你去这里: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

查询就是这么简单:获取一列的不同,更改为字符串,然后连接+下一列的字符串......

答案 2 :(得分:0)

请不要对此版本进行投票!所有的投票都应该转到上面的解决方案!下面的代码只是为了完整性。这显示了根据@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