mmalik15
asked on
how to write a Pivot table style SQL Query in this scenario
I have the following tables in my database
PerformanceObjectives(PerO bjID,Title ,etc)
Modules(ModuleID,Title)
ObjectiveModules(PerObjID, ModuleID)
Subjects(SubjectID,Title)
ModuleSubjects(ModuleID,Su bjectID)
EmployeeTrainingHours(Trai ningHoursI D,Employee ID,StartDa te,EndDate ,SubjectID ,NoOfHours )
Employee(EmployeeID,Firstn ame,Surnam e,etc).
I want my query to return the results like this
Module1 Module2 Module3 Total
Emp1 10 5 5 20
Emp2 5 7 12 24
Emp3 2 3 9 14
Now the Modules (Module1,Module2,Module3) values represent the sum of the hours of the subjects present in that module. e.g. Module1 may have sub1,sub2 and sub3. and the sum of hours spent on these subjects is 10. Similary each cell represent the total no of hours spent on each module. I am using SQL server 2005 and also taking some help from this post
https://www.experts-exchange.com/questions/23880880/how-to-get-a-timetable-like-layout-using-c.html
but unfortunately could not figure out
I will be very thankfull for any help.
PerformanceObjectives(PerO
Modules(ModuleID,Title)
ObjectiveModules(PerObjID,
Subjects(SubjectID,Title)
ModuleSubjects(ModuleID,Su
EmployeeTrainingHours(Trai
Employee(EmployeeID,Firstn
I want my query to return the results like this
Module1 Module2 Module3 Total
Emp1 10 5 5 20
Emp2 5 7 12 24
Emp3 2 3 9 14
Now the Modules (Module1,Module2,Module3) values represent the sum of the hours of the subjects present in that module. e.g. Module1 may have sub1,sub2 and sub3. and the sum of hours spent on these subjects is 10. Similary each cell represent the total no of hours spent on each module. I am using SQL server 2005 and also taking some help from this post
https://www.experts-exchange.com/questions/23880880/how-to-get-a-timetable-like-layout-using-c.html
but unfortunately could not figure out
I will be very thankfull for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
since this is using a pivot table, your query performance will be better.
Open in new window