Solved

SQL Pivot

Posted on 2013-05-21
2
407 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
2 Comments
 
LVL 40

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 97
SQL Server JOIN Question - which type to use 3 19
TSQL - IF ELSE? 3 29
SQL - SP needs a little help 9 21
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

910 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