We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

how to write a Pivot table style SQL Query in this scenario

Medium Priority
347 Views
Last Modified: 2012-05-06
I  have the following tables in my database
PerformanceObjectives(PerObjID,Title,etc)
Modules(ModuleID,Title)
ObjectiveModules(PerObjID,ModuleID)
Subjects(SubjectID,Title)
ModuleSubjects(ModuleID,SubjectID)
EmployeeTrainingHours(TrainingHoursID,EmployeeID,StartDate,EndDate,SubjectID,NoOfHours)
Employee(EmployeeID,Firstname,Surname,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

http://www.experts-exchange.com/Programming/Languages/.NET/Q_23880880.html

but unfortunately could not figure out

 I will be very thankfull for any help.








Comment
Watch Question

assuming that there are only 3 modules:
select e.EmployeeID ,
sum(
case m.ModuleID
  when 'Module1' then e.NoOfHours
  else 0
end
) as Module1,
 
sum(
case m.ModuleID
  when 'Module2' then e.NoOfHours
  else 0
end
) as Module2,
 
sum(
case m.ModuleID
  when 'Module3' then e.NoOfHours
  else 0
end
) as Module3,
 
sum(e.NoOfHours) as Total
 
from EmployeeTrainingHours e 
inner join ModuleSubjects m
on e.SubjectID = m.SubjectID
group by e.EmployeeID

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
CERTIFIED EXPERT
Awarded 2009
Distinguished Expert 2019

Commented:
This will solve your requirement:
since this is using a pivot table, your query performance will be better.
SELECT EmployeeID, ModuleID, Module1, Module2, Module3
FROM (
SELECT EM.EmployeeID, MS.ModuleID, SUM(ET.NoOfHours) TotalNoofHours
FROM Employee EM, EmployeeTrainingHours ET, ModuleSubjects MS
WHERE EM.EmployeeID = ET.EmployeeID
  AND ET.SubjectID = MS.SubjectID
GROUP BY EM.EmployeeID, MS.ModuleID) ps
PIVOT
(
max(TotalNoofHours) FOR ModuleID IN (Module1, Module2, Module3)) AS pvt

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.