2016-04-19 11 views
0

SQL Server 2005. ここではコード化された回答の後に(いいかもしれませんが)です。私は本当に必要な結果を得るために最善の方法をアドバイスしています。ピボット/アンピボット/ cte // rownumberと動的クエリに関する知識はありますが、この特定の問題を回避することはできません!データの例を次に示します。 注:タイプ、場所、名前、および記述の出現は、どれでもあり得ません。SQL - グループ化に関するアドバイス

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 
+0

単一の文字列を返したいですか? – klev

答えて

1

あなたの目標は、すべての列のすべての異なる値を選択し、1つの文字列に連結するようです。そして、あなたは唯一のアドバイスを必要とするので、私はあなたがここに行くお勧めします:multiple rows into a single row

あなたがより多くの助けが必要なように見える:あなたは4列が必要な場合は+ (SELECTクエリ

, stuff((SELECTに変更し、その後、

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; 

を1つの列を区別して文字列に変換し、次に(次の列)の文字列を連結します。

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

Thks、もう1つ注意してください:注文が必要な場合は、クエリ '' type'のコメントをすべて削除してください –

+0

こんにちは@NayruLove。ありがとう!しかし、私は複数のレコードにこれを適用する際に問題を抱えています。同様のパターンがEvent = 2、Event = 3、Event = 4などに対して繰り返されます。 – MiguelH

+0

上記のコメントを続けてください。私はすべてのタイプ、すべてのロケーション、すべての名前、すべての説明を連結しています。私が本当に後にしているのは、これらの連結を特定のキーに関連付けることです。 "キー= 1、サポート;仕事、r1、r2、fred;ジム、desc1; desc2; desc3キー= 2、これ; r6; r7、mik; ian、desc10; desc21; desc13'など – MiguelH

関連する問題