?
Solved

SQL query assistance for view

Posted on 2014-09-13
4
Medium Priority
?
173 Views
Last Modified: 2014-09-15
I have a UNIONED view with the following fields/data:

TrainingDate:          Trainee:          TotalHours:          SubDept:
1/1/2104                     John                  6.5                      Milling
1/1/2014                     John                   2                        Nailing
1/1/2014                     Jill                      7.5                     Nailing
1/2/2014                     Jill                      4.5                     Doors
1/2/2014                    John                    7.5                      Doors

I need the view with this result set:

Training Date:            Trainee:          Milling:             Nailing:            Doors:
1/1/2014                        John               6.5                     2                         0
1/1/2014                        Jill                    0                       7.5                      0
1/2/2014                        Jill                    0                       0                         4.5
1/2/2014                       John                  0                       0                        7.5

For every 'department' I would like it as a field (I believe pivot/unpivot works but I can't figure out the syntax) and for every date and trainee, there is an individual line item that shows the totals of the hours worked for that day/trainee.

Thank you :)
0
Comment
Question by:Bianca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 40321110
Try it this way:
SELECT	TrainingDate, 
	Trainee, 
	ISNULL(Milling, 0) Milling,
	ISNULL(Nailing, 0) Nailing,
	ISNULL(Doors, 0) Doors
FROM (
SELECT	TrainingDate, 
	Trainee, 
	TotalHours, 
	SubDept
FROM YourView) P
PIVOT (
SUM(TotalHours) FOR SubDept IN (Milling, Nailing, Doors)) V
ORDER BY TrainingDate, Trainee

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40321112
This is how I tested it:
DECLARE @YourView TABLE (
	TrainingDate date,
	Trainee varchar(20),
	TotalHours decimal(10, 1),
	SubDept varchar(20)
	)

INSERT	@YourView (
	TrainingDate, Trainee, TotalHours, SubDept) 
VALUES	('1/1/2104', 'John',  6.5, 'Milling'),
	('1/1/2104', 'John',  2, 'Nailing'),
	('1/1/2104', 'Jill',  7.5, 'Nailing'),
	('1/2/2104', 'Jill',  4.5, 'Doors'),
	('1/2/2104', 'John',  7.5, 'Doors')

SELECT	TrainingDate, 
	Trainee, 
	ISNULL(Milling, 0) Milling,
	ISNULL(Nailing, 0) Nailing,
	ISNULL(Doors, 0) Doors
FROM (
SELECT	TrainingDate, 
	Trainee, 
	TotalHours, 
	SubDept
FROM @YourView) P
PIVOT (
SUM(TotalHours) FOR SubDept IN (Milling, Nailing, Doors)) V
ORDER BY TrainingDate, Trainee

Open in new window

And here is the output:
TrainingDate	Trainee	Milling	Nailing	Doors
2104-01-01	Jill	0.0	7.5	0.0
2104-01-01	John	6.5	2.0	0.0
2104-01-02	Jill	0.0	0.0	4.5
2104-01-02	John	0.0	0.0	7.5

Open in new window

0
 

Author Comment

by:Bianca
ID: 40323467
I applied that to the entire data set (more departments) and I am getting an error on PIVOT although it worked when I had only 3 departments... will check data again..

SELECT      TrainingDate, Trainee, ISNULL(Milling, 0) AS Milling, ISNULL(Nailing, 0) AS Nailing, ISNULL(Doors, 0) AS Doors, ISNULL(FinalAssembly, 0) AS FinalAssembly, ISNULL(Eng, 0) AS Eng, ISNULL(FaceFrame, 0) AS FaceFrame, ISNULL(LineSanding, 0) AS LineSanding, ISNULL(CDR, 0) AS CDR,
ISNULL(Finishing, 0) AS Finishing, ISNULL(Storage, 0) AS Storage, ISNULL(Molding, 0) AS Molding
PIVOT(SUM(TotalHours) FOR SubDept IN (Milling, Nailing, Doors, FinalAssembly, Eng, FaceFrame, LineSanding, CDR, Finishing, Storage, Molding)) AS V
ORDER BY TrainingDate, Trainee
0
 

Author Comment

by:Bianca
ID: 40323531
My typo.. WORKS GREAT!  
Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question