Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Multiple select and sum expressions on the same tb

Author  Topic 

ricc
Starting Member

16 Posts

Posted - 2005-10-25 : 09:48:14
Little complex but I'll try to explain best I can
I have the following table…

UserID___Year___Month___A1____A2____B1___B2
__1_______4______5______45_____5____43____0
__1_______4______6______10_____12___1_____3
__1_______5______5______12_____22___8_____7
__1_______5______6______11_____10___5_____0

__2_______4______5______9______65___33____66
__2_______4______6______3______3____10____11
__2_______5______5______44_____2___77_____1


I want to pull out the data to show the sum of the totals for each year the user is in. The idea is to then be able to see a comparison for year on year on a month by month basis
Eg.

UserID____TotalAYear4____TotalBYear4___TotalAYear5____TotalBYear5___Month
__1___________50_____________43___________34____________15__________5
__1___________22_____________4____________21____________5___________6
__2___________74_____________99___________21____________5___________5


There would be no data returned for UserID2 for month 6 as there is no data for them for year 5 month 6 therefore no comparison can be made.

I’ve having real difficulty getting the correct SQL syntax to do this. Any ideas or pointers would be great thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-25 : 09:49:14
Refer this cross tab
http://weblogs.sqlteam.com/jeffs/archive/2005/05.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -