Solved

SQL query assistance for view

Posted on 2014-09-13
4
172 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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