PIVOT语句中的TSQL错误

时间:2017-03-15 17:45:14

标签: sql sql-server tsql pivot

这是我在PIVOT语句中给出错误的脚本。我疯了,不知道那里有什么问题。有人可以帮忙吗?

SELECT p.id, p.ln, p.fn, p.sc, p.gr, p.TestName, p.testgrade, 
              'CA Eng. Lang. Dev. Test' as Overall, 'List. & Speaking' as Speak, 'Reading',
                                                         'Writing', 'Listening', 'Speaking', 'Comprehension', 'CELDT Criterion' as CELDT_criterion
  FROM
              (
                     select *
                     from (
                                  select s.id,s.ln,s.fn,s.sc, s.gr
                          , t.id as TestName
                          , (t.gr/10) as testgrade
                          , CONVERT(varchar,t.td,101) as testdate
                          --, t.pt as testpart -- 0 is overall
                          , c.nm as testdesc
                          , t.ss as testscore
                          --, t.ot as profLevel                              
                                  , row_number() over (partition by c.nm,DATEPART(MM, t.td) order by t.td desc) as rn
                                  from tst t
                                  JOIN Stu s ON s.id = t.pid
                                  JOIN ctl c ON t.id = c.id and t.pt = c.pt 
                                  where
                                  t.PID = 2062921 and
                                  s.tg = ' ' and 
                                  t.ID in ( 'CELDT') 
                      ) t2
                     where t2.rn = 1
              ) s
PIVOT
(
  MAX(testscore) FOR testdesc IN ('CA Eng. Lang. Dev. Test', 'List. & Speaking', 'Reading',
                                                         'Writing', 'Listening', 'Speaking', 'Comprehension', 'CELDT Criterion')
  ) p

1 个答案:

答案 0 :(得分:2)

我认为这就是你想要的。请记住在数据透视表中使用[和]指定字段名称,这些名称实际上是您的测试名称。另外我不得不说出testpart,因为那不是唯一的,这导致了很多行(来自枢轴)。

SELECT p.id, p.ln, p.fn, p.sc, p.gr, p.TestName, p.testgrade, 
          [CA Eng. Lang. Dev. Test] as Overall, [List. & Speaking] as Speak, [Reading],
                                                     [Writing], [Listening], [Speaking], [Comprehension], [CELDT Criterion] as CELDT_criterion
FROM
          (
                 select *
                 from (
                              select s.id,s.ln,s.fn,s.sc, s.gr
                              , t.id as TestName
                              , (t.gr/10) as testgrade
                              , CONVERT(varchar,t.td,101) as testdate
                              --, t.pt as testpart -- 0 is overall
                              , c.nm as testdesc
                              , t.ss as testscore
                              --, t.ot as profLevel
                              , row_number() over (partition by c.nm,DATEPART(MM, t.td) order by t.td desc) as rn
                              from tst t
                              JOIN Stu s ON s.id = t.pid
                              JOIN ctl c ON t.id = c.id and t.pt = c.pt 
                              where
                              t.PID = 2062921 and
                              s.tg = ' ' and 
                              t.ID in ( 'CELDT') 
                  ) t2
                 where t2.rn = 1
          ) s
PIVOT
(
  MAX(testscore) FOR testdesc IN ([CA Eng. Lang. Dev. Test], [List. & Speaking], [Reading],
                                                     [Writing], [Listening], [Speaking], [Comprehension], [CELDT Criterion])
 ) p