将表的多个列组合为单个列,其中第一列值确定新行

时间:2012-01-13 20:46:29

标签: tsql

基本上我需要从表中取3列,然后使用第一列的值将它们组合成字符串行。

使用以下方式启动基本输出:

    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. 41201222012320126201282012
 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

现在在上面做的时候将所有类似的初始行组合起来......

2 个答案:

答案 0 :(得分: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

我使用了row_number窗口函数来获取每一行的顺序,然后将每一行转移到它自己的列,然后转换为varchar,然后按itemid分组后连接到varchar

答案 1 :(得分: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 @str=@str+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)+@str

end

select *
from @results

创建:

412012220122201232012420126201282012
121201222012420126201272012
538201292012