数据透视表并将记录合并为1行

时间:2013-08-15 15:08:26

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

我尝试了许多不同的方法来旋转表格来显示1行的所有记录。我已经提供了我想出的最接近的解决方案的查询。如果我说明我需要什么,这可能会更容易。由于可以存在无限数量的教师调查问题,因此查询必须是动态的。我修改了列名,使其更易于阅读。

teacherSurveyQuestions

TSQID    CID    Order    OQReference    Stem
1        1011   1        q1_rb          blabla
2        1011   2        q2_rb          blabla
3        1011   3        q2a_cb         blabla

teacherSurveyUserID

TSUID    firstName    lastName    UID
1        Bob          Smith       1027
2        Tom          Jones       1034

teacherSurveyAnswers

TSAID    UID    TSQID    TSUID    Response
1        1027   1        1        Bob 1
2        1027   2        1        Bob 2
3        1027   3        1        Bob 3
4        1034   1        2        Tom 1
5        1034   2        2        Tom 2
6        1034   3        2        Tom 3

现在我需要这样的数据:

firstName    lastName    q1_rb    q2_rb    q2a_cb
Bob          Smith       Bob 1    Bob 2    Bob 3
Tom          Jones       Tom 1    Tom 2    Tom 3

这是我到目前为止所做的工作,除了所有的响应都是NULL

declare  @query as nvarchar(max),
         @colsPivot as nvarchar(max)

select @colsPivot = stuff((select ',' 
                           + quotename(OQReference) 
                           from teacherSurveyQuestions tsq
                           where tsq.CID = 1011
                           order by tsq.Order
                    for xml path(''), type
                    ).value('.', 'nvarchar(max)') 
                    ,1,1,'')

set @query 
= 'select *
from
  (
    select firstName, lastName, value, col +''_''+ CAST(rn as varchar(10)) as col
    from
      (
       select
         tsu.TSUID  
         ,tsu.firstName
         ,tsu.lastName
         ,tsq.OQReference
         ,tsa.Response
         ,ROW_NUMBER() over(partition by tsu.TSUID order by tsq.Order) rn
       from teacherSurveyQuestions tsq 
       inner join teacherSurveyAnswers tsa on tsa.TSQID = tsq.TSQID
       inner join teacherSurveyUsers tsu on tsu.TSUID = tsa.TSUID
       where tsq.CID = 1011
   ) x
   unpivot
   (
     value
     for col in (OQReference)
   ) u
  ) x1
  pivot
  (
    max(value)
    for col in ('+ @colspivot +')
  ) p'

exec(@query)

查询结果:

firstName    lastName    q1_rb    q2_rb    q2a_cb
Bob          Smith       NULL     NULL     NULL
Tom          JOnes       NULL     NULL     NULL

1 个答案:

答案 0 :(得分:0)

试试这个

declare  @query as nvarchar(max),
         @colsPivot as nvarchar(max)

select @colsPivot = stuff((select ',' 
                           + quotename(OQReference) 
                           from teacherSurveyQuestions tsq
                           where tsq.CID = 1011
                           order by tsq.[Order]
                    for xml path(''), type
                    ).value('.', 'nvarchar(max)') 
                    ,1,1,'')



set @query 
= 'select firstName, lastName,'+ @colspivot +'
from
  (
    select firstName, lastName,Response, value
    from
      (
       select
         tsu.TSUID  
         ,tsu.firstName
         ,tsu.lastName
         ,tsq.OQReference
         ,tsa.Response
         ,ROW_NUMBER() over(partition by tsu.TSUID order by tsq.[Order]) rn
       from teacherSurveyQuestions tsq 
       inner join teacherSurveyAnswers tsa on tsa.TSQID = tsq.TSQID
       inner join teacherSurveyUserID tsu on tsu.TSUID = tsa.TSUID
       where tsq.CID = 1011
   ) x
   unpivot
   (
     value
     for col in (OQReference)
   ) u
  ) x1
  pivot
  (
    max(Response)
    for value in ('+ @colspivot +')
  ) p'

exec(@query)

SQL FIDDLE DEMO