如何将1列值转换为多列

时间:2012-08-20 05:37:07

标签: sql sql-server

假设我有一个表“标题”在此表中有4列。

Bookid, Isbn, TitleName, Description.

1         123  english     this is a english buk.

2        123  english      Its author is "Mr xyz"

现在你看到b'coz描述同一本书出现了2次。

现在我们如何才能得到以下输出.Plese指南

Bookid, Isbn,         TitleName,           Description1,                  Description2

1         123         english              "this is a english buk"         "Its author is Mr xyz"

4 个答案:

答案 0 :(得分:2)

根据您的评论,听起来您想要一种动态的方式来产生这些结果。您可以使用动态SQL生成PIVOT。使用动态SQL将允许您自动将其他描述添加到结果中。

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME('Description'+cast(row_number() over (partition by bookid, isbn, titlename order by bookid) as varchar(10))) 
                    from title
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
      = 'SELECT bookid, isbn, titlename,' + @cols + ' from 
         (
            select bookid, isbn, titlename, description,
              ''Description''+ cast(row_number() over (partition by bookid, isbn, titlename order by bookid) as varchar(10)) rn
            from title
         ) x
         pivot 
         (
            min(description)
            for rn in(' + @cols + ')
         ) p '

execute(@query)

请参阅SQL Fiddle with Demo

在为书籍添加新描述时,执行查询时会在结果中添加新列。

答案 1 :(得分:1)

您可以使用子查询来计算每本书的最小和最大BookId。然后你可以加入两个描述:

select  min(BookId)
,       Isdn
,       TitleName
,       max(case when rn = 1 then Description end) as Descr1
,       max(case when rn = 2 then Description end) as Descr2
,       max(case when rn = 3 then Description end) as Descr3
from    (
        select  row_number() over (partition by Isdn, TitleName order by BookId) as rn
        ,       BookId
        ,       Isdn
        ,       TitleName
        ,       Description 
        from    Title
        ) as ids
group by
        Isdn
,       TitleName

答案 2 :(得分:0)

试试这个

 select * 
 from
 (
      select 
           *,
           row_number() over (partition by bookid, isbn, titlename order by description) rn
      from yourtable
 ) src
 pivot
 ( max(description) for rn in ([1],[2],[3])) p

答案 3 :(得分:0)

@bluefeet,我修改了你的查询以允许超过10个描述(已排序)....

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((select c from (SELECT  ',' + QUOTENAME('Description'+cast(row_number() over (partition by bookid, isbn, titlename order by bookid) as varchar(11))) AS C
                    from title) TBL group by c order by CAST(Replace(REPLACE(C,',[Description',''),']','') as int)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
      = 'SELECT bookid, isbn, titlename,' + @cols + ' from 
         (
            select bookid, isbn, titlename, description,
              ''Description''+ cast(row_number() over (partition by bookid, isbn, titlename order by bookid) as varchar(10)) rn
            from title
         ) x
         pivot 
         (
            min(description)
            for rn in(' + @cols + ')
         ) p '

execute(@query)