Solved

SQL query assistance for view

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now