Flip Booth
Flip Booth

Reputation: 271

SQL Pivot issues. Getting wrong output

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

My Query:

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

The table is shown above. I tried everything! I'm not getting the result I want. I want it to be pivoted. How do I do this? I am on SQL server 2008. Thanks in advance!

Upvotes: 0

Views: 64

Answers (1)

Andriy M
Andriy M

Reputation: 77677

Just remove grouping and aggregation from the subquery:

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

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

Upvotes: 2

Related Questions