Solved

SQL Pivot

Posted on 2013-05-21
2
415 Views
Last Modified: 2013-05-21
Sharath provided me a Pivot Solution at This Link

But I'm struggling to get this to pivot for me.

Basically I need the totals by state columns

Right now, this select
SELECT	HT.[YR],
		A.[Specialty],
		A.[Lead ID],
		A.[Client State] AS ClientState,
		HT.[HRS]
FROM	[dbo].[Assignment] A
INNER JOIN 
	(
		SELECT	[Assignment Number], SUM(ISNULL(H.[Hours],0) + ISNULL(H.[On Call Hours],0)) HRS, YEAR(H.[Week Ending Date]) AS [YR]
		FROM	[dbo].[HoursTracking] H
		GROUP BY
				[Assignment Number] ,YEAR(H.[Week Ending Date])        
	) HT
ON	A.[Assignment Number] = HT.[Assignment Number]
WHERE	HT.HRS > 0 AND A.[Lead ID] IS NOT NULL
ORDER BY A.[Client State],A.[Specialty],  HT.[YR] DESC

Open in new window


Produces this

sp

What I need to see is this
The states are the sum of that state for that specialty and that year
Of course there will be all the states.
ssw
0
Comment
Question by:lrbrister
[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 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39185614
can you try this?
declare @select varchar(max)
select @select = STUFF((select distinct ',' + quotename(StateName, '')
from States
for XML PATH('')
),1,1,'' )

select @select = 'select YR,Specialty,'+@select+
   ' from (
		SELECT	HT.[YR],
		A.[Specialty],
		A.[Lead ID],
		A.[Client State] AS ClientState,
		HT.[HRS]
FROM	[dbo].[Assignment] A
INNER JOIN 
	(
		SELECT	[Assignment Number], SUM(ISNULL(H.[Hours],0) + ISNULL(H.[On Call Hours],0)) HRS, YEAR(H.[Week Ending Date]) AS [YR]
		FROM	[dbo].[HoursTracking] H
		GROUP BY
				[Assignment Number] ,YEAR(H.[Week Ending Date])        
	) HT
ON	A.[Assignment Number] = HT.[Assignment Number]
WHERE	HT.HRS > 0 AND A.[Lead ID] IS NOT NULL) t1
 pivot (sum(HRS) for ClientState in ('+@select+')) p'
 exec(@select)

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 39185639
Excellent! Thanks

That's much better that what I manually came up with


SELECT [YR],[Specialty], [AK],[AL],[AR],[AZ],[CA],[CO],[CT],[DC],[DE],[FL],[GA],[HI],[IA],[ID],[IL],[IN],[KS],[KY],[LA],[MA],[MD],[ME],[MI],[MN],[MO],[MS],[MT],[NC],[ND],[NE],[NH],[NJ],[NM],[NV],[NY],[OH],[OK],[OR],[PA],[RI],[SC],[SD],[TN],[TX],[UT],[VA],[VI],[VT],[WA],[WI],[WV],[WY]
FROM
      (
            SELECT      HT.[YR],
                        A.[Specialty],
                        A.[Lead ID],
                        A.[Client State] AS ClientState,
                        HT.[HRS]
            FROM      [dbo].[Assignment] A
            INNER JOIN
                  (
                        SELECT      [Assignment Number], SUM(ISNULL(H.[Hours],0) + ISNULL(H.[On Call Hours],0)) HRS, YEAR(H.[Week Ending Date]) AS [YR]
                        FROM      [dbo].[HoursTracking] H
                        GROUP BY
                                    [Assignment Number] ,YEAR(H.[Week Ending Date])        
                  ) HT
            ON      A.[Assignment Number] = HT.[Assignment Number]
            WHERE      HT.HRS > 0 AND A.[Lead ID] IS NOT NULL
      ) p
PIVOT
(
      SUM(P.[HRS])
      FOR P.ClientState IN ([AK],[AL],[AR],[AZ],[CA],[CO],[CT],[DC],[DE],[FL],[GA],[HI],[IA],[ID],[IL],[IN],[KS],[KY],[LA],[MA],[MD],[ME],[MI],[MN],[MO],[MS],[MT],[NC],[ND],[NE],[NH],[NJ],[NM],[NV],[NY],[OH],[OK],[OR],[PA],[RI],[SC],[SD],[TN],[TX],[UT],[VA],[VI],[VT],[WA],[WI],[WV],[WY])
) AS myPIVOT

GROUP BY [YR],[Specialty]
ORDER BY [YR],[Specialty]
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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