0

我正在研究一个枢轴问题。我想出了一些代码,但未能成功编写代码。请有人能给我一些关于我在这里做错了什么的指导吗?

我有表 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:如果您需要做一些特别的事情来让任何人回答/回复您的帖子,请随时在您的回复中分享。

4

3 回答 3

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 演示

于 2013-07-29T13:43:15.220 回答
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未定义
于 2013-07-29T13:36:05.380 回答
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
于 2013-07-30T10:24:23.183 回答