0

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

教师调查问题

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

教师调查用户ID

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

教师调查答案

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

这是我到目前为止所拥有的那种作品,除了所有的回应都是空的

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
4

1 回答 1

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 小提琴演示

于 2013-08-15T17:05:41.617 回答