1

Suppose if i have one table "Title" In this table there are 4 columns.

Bookid, Isbn, TitleName, Description.

1         123  english     this is a english buk.

2        123  english      Its author is "Mr xyz"

Now you see that b'coz of description same book appear 2 times.

Now how can we get the below output.Plese Guide

Bookid, Isbn,         TitleName,           Description1,                  Description2

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

4 回答 4

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

当您为书籍添加新描述时,执行查询时将在结果中添加一个新列。

于 2012-08-20T11:44:18.997 回答
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
于 2012-08-20T05:40:14.953 回答
0

Try this

 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
于 2012-08-20T11:34:30.587 回答
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)
于 2013-11-26T18:04:28.730 回答