T-SQL Query Rows into Columns with Sums

I have a table that looks like:

studentID int
Year int
Month int
ClassType int
ClassTypeCount int

It's the result of a query producing counts by year, month and classtype.  

In order to preserve compatability, I have to create a view turning the rows into month columns providing a sum of counts from a combination of ClassTypes:

Roughly:

CASE WHEN month = 8 THEN (SELECT SUM(ClassTypeCount) WHERE ClassType IN (1000, 1002, 1003) and month = 8 And year = <the year of this row> and student = <the student of this row>)





LVL 2
naisnetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
-- something like this?

select StudentID, Year, ClassType
   SUM( case when Month=7 then ClassTypeCount else 0 end ) as July
   SUM( case when Month=8 then ClassTypeCount else 0 end ) as August
   SUM( case when Month=9 then ClassTypeCount else 0 end ) as September  -- a row like this for each month
   -- etc
)
from myTable
group by StudentID, Year, ClassType
naisnetAuthor Commented:
Thanks for the quick response!

Sort of, but it's one step beyond that.  

I have to group the sums in classTypes.  Here's a better example:

Raw Table Data:
StudentID ClassType ClassTypeCount Year Month
1000         1005        12                    2005   7
1000         1006         9                     2005   7

I need to convert that to (in this example, I'm, grouping 1005 and 1006 classtypes).

StudentID  Year   jan feb mar apr may jun jul aug sept oct nov dec
1000          2005  0   0    0     0    0     0    21  0     0     0    0     0

Hope that's a slightly clearer explanation.  Thanks again!


naisnetAuthor Commented:
PS The rows would be evaluated, I'm not grouping the whole set, I group classtypes (1005, 1006) as 'Class A' and ClassTypes(1010, 1020, 1030) as 'Class B'.  

Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

knightEknightCommented:
-- Well, this will work for one specific classtype:

select StudentID, Year
   SUM( case when Month=7 then ClassTypeCount else 0 end ) as July
   SUM( case when Month=8 then ClassTypeCount else 0 end ) as August
   SUM( case when Month=9 then ClassTypeCount else 0 end ) as September  -- a row like this for each month
   -- etc
)
from myTable
where ClassType in ( 1005, 1006 )  -- change this to get a different class type
group by StudentID, Year



-- How many class type groupings do you have?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
You should be able to do this with a PIVOT query.  Let me know if you are interested.
knightEknightCommented:
I am.  :)
Anthony PerkinsCommented:
Oops, I am sorry, I missed that.
naisnetAuthor Commented:
I actually ended up working it out by using a pivot table and a few case statements.  Thank you all for your help.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.