PIVOT 语句中的 TSQL 错误
TSQL Error in PIVOT statement
这是我的脚本,在 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
我想这就是你想要的。请记住,在数据透视表中,您使用 [ 和 ] 指定字段名称,这实际上是您的测试名称。此外,我还必须注释掉测试部分,因为它不是唯一的,这导致了很多行(来自数据透视表)。
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
这是我的脚本,在 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
我想这就是你想要的。请记住,在数据透视表中,您使用 [ 和 ] 指定字段名称,这实际上是您的测试名称。此外,我还必须注释掉测试部分,因为它不是唯一的,这导致了很多行(来自数据透视表)。
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