SELECT * from
(
SELECT intUserID as [KidId], intAssessmentID as [assid] ,vchTopic as [Topics] from ManageAssessment WHERE intUserID=174 and IsRorW='yes'
)AS S
pivot
(
COUNT(KidId)
FOR assid IN ([22],[19],[16],[17],[18])
)AS s
嘿我的代码创建一个表我想添加其他列,但它错了..! 我想要第4列后面的一列,即2值的差异。任何人都可以帮我解决..谢谢..... enter image description here
答案 0 :(得分:2)
你可以这样做:
SELECT
Topics,
[22],
[19],
[16],
[17],
[18],
[22] - [19] AS NewColumn
from
(
SELECT
intUserID as [KidId],
intAssessmentID as [assid] ,
vchTopic as [Topics]
from ManageAssessment
WHERE intUserID=174 and IsRorW='yes'
)AS S
pivot
(
COUNT(KidId)
FOR assid IN ([22], [19], [16], [17], [18])
)AS s;
如果要为所有新列添加新行作为总计,可以执行以下操作:
WITH CTE AS
(
SELECT
Topics,
[22],
[19],
[16],
[17],
[18],
[22] - [19] AS NewColumn
from
(
SELECT
intUserID as [KidId],
intAssessmentID as [assid] ,
vchTopic as [Topics]
from ManageAssessment
WHERE intUserID=174 and IsRorW='yes'
)AS S
pivot
(
COUNT(KidId)
FOR assid IN ([22], [19], [16], [17], [18])
)AS s
)
SELECT
Topics,
[22],
[19],
[16],
[17],
[18],
NewColumn
FROM CTE
UNION ALL
SELECT
'Total',
SUM([22]),
SUM([19]),
SUM([16]),
SUM([17]),
SUM([18]),
SUM(NewColumn)
FROM CTE;