Solved

SQL query assistance for view

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Analysis of table use 7 47
SQL Restore Script - Syntax Error 8 97
MS SQLK Server multi-part identifier cannot be bound 5 35
kill process lock Sql server 9 47
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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