2012-01-13 15 views
0

基本的には、テーブルから3カラムを取り出し、最初のカラムの値を使用してストリングの行に結合する必要があります。テーブルの複数のカラムを1つのカラムに結合すると、最初のカラム値が新しいローを決定します

使用して基本的な出力の開始:

select ItemID, Month, Year 
     from tableA 
     where ID = @id 

出力:

ItemID Month Year 

1. 4 1 2012 
2. 4 2 2012 
3. 4 3 2012 
4. 4 6 2012 
5. 4 8 2012 
6. 12 1 2012 
7. 12 2 2012 
8. 12 4 2012 
9. 12 5 2012 
10. 12 6 2012 
11. 12 7 2012 
12. 53 8 2012 
13. 53 9 2012 

私はこれと似たものになります取得しようとしています出力:基本的に

1. 41201222
2. 12120122201242012520126201272012 
3. 538201292012 

を、同じ番号のすべてのItemIDを結合し、新しいItemIDがあるときに新しい行を開始します。

私はさまざまなテンポラリテーブルとピボットを試しましたが、これを正しく出力することはできません。どんな助けもありがとう。

おかげで、これは使用したので

、:

declare @table table (idmy varchar(8)) 

insert into @table 
    select cast(ItemID as varchar(2)) + 
     cast(Month as varchar(2)) + 
     cast(Year as varchar(4)) as idmy 
     from TableA 
      where ID = @id 

select idmy from @table 

私はの出力を得ることができます:上記のことながら、今、すべてのような初期の行を組み合わせること

idmy 
1. 412012 
2. 422012 
3. 432012 
4. 462012 
5. 482012 
6. 1212012 
7. 1222012 
8. 1242012 
9. 1252012 
10. 1262012 
11. 1272012 
12. 5382012 
13. 5392012 

を...

+0

可能[MS SQL Server 2005でのgroup_concatのMySQL機能のシミュレーション?](http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-ms-sql-server-2005) –

答えて

0
create table #tableA 
(ItemID int, Month int, Year int) 

insert into #tableA 
values (4, 1, 2012) 
insert into #tableA 
values (4, 2, 2012) 
insert into #tableA 
values (4, 2, 2012) 
insert into #tableA 
values (4, 3, 2012) 
insert into #tableA 
values (4, 4, 2012) 
insert into #tableA 
values (4, 6, 2012) 
insert into #tableA 
values (4, 8, 2012) 
insert into #tableA 
values (12, 1, 2012) 
insert into #tableA 
values (12, 2, 2012) 
insert into #tableA 
values (12, 4, 2012) 
insert into #tableA 
values (12, 6, 2012) 
insert into #tableA 
values (12, 7, 2012) 
insert into #tableA 
values (53, 8, 2012) 
insert into #tableA 
values (53, 9, 2012) 



declare @results as table 
(line varchar(max)) 

declare @currentItemId int 
set @currentItemId = -1 

declare @str varchar(max) 

while(@currentItemId is not null) 
begin 

    set @str = '' 

    select @currentItemId=min(ItemID) 
    from #tableA 
    where @currentItemId < ItemID 

    if(@currentItemId is null) 
     break 

    select @[email protected]+cast(Month as varchar)+cast(Year as varchar) 
    from #tableA 
    where ItemID = @currentItemId 
    order by Year, Month 

    insert into @results 
    select cast(@currentItemId as varchar)[email protected] 

end 

select * 
from @results 

によってグループ化された後に連結された上方VARCHARに変換される独自の列にそれぞれを回動する機能を窓掛けROW_NUMBER作成:

4120122201222
121201222012420126201272012 
538201292012 
+0

この回答は意図どおりに機能し、私が必要としていたものであり、非常に便利です。ご回答いただきありがとうございます。 –

+0

しかし、私のシニアプログラマは、SQL SPのwhileループの使用について眉をひそめました。誰かがこのことを読んでいれば、SQLストアドプロシージャのループをどう思いますか? –

+0

ときどきシンプルな方が良い場合もあり、時にはループも関係します。 – JBrooks

0
select 
coalesce(cast(itemid as nvarchar(4)),'') + 
coalesce(cast([1] as nvarchar(500)),'') + 
coalesce(cast([2] as nvarchar(500)),'') + 
coalesce(cast([3] as nvarchar(500)),'') + 
coalesce(cast([4] as nvarchar(500)),'') + 
coalesce(cast([5] as nvarchar(500)),'') + 
coalesce(cast([6] as nvarchar(500)),'') + 
coalesce(cast([7] as nvarchar(500)),'') data 
from 
(select itemid, 
    cast(Month as varchar(2)) + 
    cast(Year as varchar(4)) as idmy, 
    row_number() over (partition by itemid order by cast(ItemID as varchar(2)) + cast(Month as varchar(2)) + cast(Year as varchar(4))) rownum 
from test 
) f 
pivot (max(idmy) for rownum in ([1], [2], [3], [4], [5], [6], [7])) p 

私は彼は順番に各列を取得し、その後、アイテムID

+0

ほとんどすべての行の挿入でItemIDを繰り返すことを除いて、ほとんどの場合と同様に動作します。それは確かに、応答のおかげで有益です。 –

+0

最初にitemidを入力するように編集しました。ここでの制限は、ピボット・ステートメントを拡張し、アイテムごとの最大行数を予測する必要があることです。現在、アイテムごとに7行の上限が設定されています。 – Adrian

関連する問題