数据透视表代码,需要一些想法

时间:2013-07-29 13:19:29

标签: sql sql-server sql-server-2008-r2 pivot

我正在研究Pivot问题。我提出了一些代码,但是在编写代码时没有成功。请问有人可以给我一些关于我在这里做错的指导吗?

我有表1,它在下面的代码中创建:

create table T1 (
     [name]  varchar(30)
    ,[size]  int
    ,[DT]  date)

 insert into T1 values ( 'x1', 14,  '01/03/2013' );
 insert into T1 values ( 'x1', 134, '01/04/2013' );
 insert into T1 values ( 'x1', 199, '01/05/2013' );
 insert into T1 values ( 'x1', 284, '01/06/2013' );
 insert into T1 values ( 'x2', 212, '01/03/2013' );
 insert into T1 values ( 'x2', 369, '01/04/2013' );
 insert into T1 values ( 'x2', 439, '01/05/2013' );
 insert into T1 values ( 'x2', 555, '01/06/2013' );

我需要将表格转换为这种格式:

    01/03/13  01/04/2013    01/05/2013   01/16/2013
X1    14            134        199         284
X2    212           369        439         555

这是我一直在研究的代码,但是在上述输出中没有成功?对我有什么想法或指示?提前谢谢......

declare @DateList as varchar(max)
declare @dynamic_PQ as varchar(max)

select @DateList = 
  stuff(      (
select DISTINCT  
      ', '+ Quotename(CONVERT(VARCHAR(10),DT,110))
    from
    (       select dt  from t1 ) t
   for xml path ('')
   ),1,1,'')

  select @DateList 

 set @dynamic_PQ = 'select [GuestID], ' + @DateList + 
  ' from
    (
    Select [name],
      size,
      STUFF((SELECT distinct '', '' + convert(a2.size as varch(10))
               from t1 a2
               where src.name = a2.name
                  and src.dt = a2.dt
                  FOR XML PATH(''''), TYPE
                  ).value(''.'', ''NVARCHAR(MAX)'') 
              ,1,1,'''') answer
    from
    (
      select name,
        dt ,
        size
      from t1
    ) src
   ) as S
   PIVOT

   (
    MAX([size])
    for Question IN (' + @DateList + ')
   ) as P

   Exec(@dynamic_PQ)

PS:如果有什么特别的事情需要让任何人回复/回复你的帖子,请随时在你的回复中分享。

3 个答案:

答案 0 :(得分:1)

你有未闭合的引号,额外不需要的括号,不需要动态sql内的STUFF FOR XML的整个部分(对于输出描述),列名错误...

declare @DateList as varchar(max)
declare @dynamic_PQ as varchar(max)

select @DateList = 
  stuff(      (
select DISTINCT  
      ', '+ Quotename(CONVERT(VARCHAR(10),DT,110))
    from
    (       select dt  from t1 ) t
   for xml path ('')
   ),1,1,'')

  --select @DateList 

 set @dynamic_PQ ='SELECT * from
    (
      select name,
        dt ,
        size
      from t1
    ) as S
   PIVOT
   (
    MAX([size])
    for DT IN ('+ @DateList +')
   ) as P'



   EXEC (@dynamic_PQ)

<强> SQLFiddle DEMO

答案 1 :(得分:0)

您的查询有多个错误。这个版本有效:

select [name], [01-03-2013], [01-04-2013], [01-05-2013], [01-06-2013]
from ( Select [name], size,
       STUFF((SELECT distinct ', ' + cast(a2.size as varchar(10))
              from t1 a2
              where src.name = a2.name and src.dt = a2.dt
              FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,''
              ) as question
       from ( select name, dt , size
              from t1
            ) src
     ) S
PIVOT ( MAX([size]) for Question IN ([01-03-2013], [01-04-2013], [01-05-2013], [01-06-2013])
      )as pvt;

您应该通过打印SQL并运行它来调试它。以下是一些问题:

  • 您在数据透视表中使用question,但在代码中定义answer
  • 表达式convert(a2.size as varch(10))是非敏感的SQL
  • pivot语句需要别名
  • 未定义列GuestId

答案 2 :(得分:0)

这是一个简单的查询

select name, [01-03-2013], [01-04-2013], [01-05-2013], [01-06-2013] from
        (select  name, size, DT from T1)a
        PIVOT(sum(size) for DT in ([01-03-2013], [01-04-2013], [01-05-2013], [01-06-2013])) as pvt