SQL Pivot问题。输出错误

时间:2012-10-29 17:06:30

标签: sql

TCCode JobCode Totals
------ ------- -----------
L402   A       1
L402   F       16
L402   H       1
L402   S       12
L402   W       12
L603   A       1
L603   F       5
L603   S       8
L603   W       8

我的查询:

SELECT * FROM
(
Select TCCode, JobCode, count(*) AS Totals 
From myTable
Group By JobCode, TCCode 
) t1

pivot ( Count (JobCode) for JobCode in ([A], [S], [H], [F], [W])) as Totals

表格如上所示。我尝试了一切!我没有得到我想要的结果。我希望它可以转动。我该怎么做呢?我在SQL Server 2008上。提前感谢!

1 个答案:

答案 0 :(得分:2)

只需从子查询中删除分组和聚合:

SELECT * FROM
(
Select TCCode, JobCode
From myTable
) t1

pivot ( Count (JobCode) for JobCode in ([A], [S], [H], [F], [W])) as Totals