Solved

SQL query assistance for view

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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