Solved

Pivot Query

Posted on 2013-06-27
12
224 Views
Last Modified: 2013-06-28
I am trying to write a query to pivot this data.
Pivot.txt
0
Comment
Question by:proginc
[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
  • 8
  • 4
12 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39281654
could you spend some time explaining this?
Original Data

DeviceID 	DeviceName	Group			Permission 	Pending
000001		MainOffice	Users	  		  1		   1	 
000011		MainOffice	Customers	  	  1		   0	 
000021		MainOffice	Vendors	  	  	  0		   1	 
000002		Office1		Customers 		  1		   1
000003		Office2		Vendors 		  1		   0 
000004		Office3		SpecialAccess		  0		   1
000014		Office3		Users		  	  1		   1
000024		Office3		Customers		  1		   1
000034		Office3		Vendors		  	  0		   0



Pivot Data


DeviceID 	DeviceName	Users	Customers   Vendors	SpecialAccess	Etc...
000091		MainOffice	  2	    1	       4              0        
000092		Office1		  0 	    2          0	       0 
000093		Office2		  0 	    0	       1  	       0
000094		Office3		  1         1	       3	       4 


1 - Allowed & Not Pending
2 - Allowed & Pending
3 - Denied & Not Pending
4 - Denied & Pending

Open in new window

e.g. How do you arrive at DeviceID 000091,92,93,94 from that data?
What are the numbers under the pivot headings? a SUM()? a COUNT()? something else?
is it sum(Permission + Pending) perhaps?
does Etc. mean there is more headings?

What is the relevance of the last 4 codes?

are you expecting dynamic sql?
what version of sql server is this for?
0
 
LVL 1

Author Comment

by:proginc
ID: 39281931
My apologies for not giving any details.

The deviceID can be ignored, I just need a unique identifier. The exiting query gives me this result in the first table. I need to change that so i get the result listed in the second table.

The pivot headings are not sum or count but uses the rule specified below the table. Allowed and Denied is from the Permissions column. The list of groups can be 5 or 10, but i need only the top 5.

MS Sql 2005
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39282004
thanks, still would like to know about the "etc." and if this needs to be dynamic

If there is a finite number of headings then it does not need to be dynamic of course.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:proginc
ID: 39282221
yes it has to be dynamic, there will be infinite "groups", however we need to list the first 5.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39283274
oh dear, what does "first 5" mean to you?
i.e. how would the SQL know what you are thinking here?

nb: "TOP 5" could be totally wrong
0
 
LVL 1

Author Comment

by:proginc
ID: 39283340
You are correct, there is no way to determine first 5, and no criteria to select the Top 5, best way would be to select all groups.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39283609
There are some unresolved issues in the following:
1. Including DeviceID in the final result is a problem - it produces too many rows
2. I have not found a way to provide zero instead of null in the final output
3. if I don't order the pivot headings it will be unpredictable (so different to expected)
4. I think there are errors in the expected results provided

* believe these are incorrect
Expected:
DeviceID 	DeviceName	Users	Customers   Vendors	SpecialAccess	Etc...
000091		MainOffice	  2	    1	       4              0        
000092		Office1		  0 	    2          0	       0 
000093		Office2		  0 	    0	       1  	       0
000094		Office3		  1*        1*         3               4 

so far achieved:
DEVICENAME	CUSTOMERS	SPECIALACCESS	USERS	VENDORS
MainOffice	1		(null)		2	4
Office1		2		(null)		(null)	(null)
Office2		(null)		(null)		(null)	1
Office3		2		4		2	3

Open in new window

The code to produce the above is this:
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @query  AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME([Group]) AS COLORD
            FROM OrigData
            ORDER BY COLORD
            FOR XML PATH('')
                   , TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT DeviceName, ' + @cols + ' from 
            (
                select
                      DeviceName
                    , [Group]
                    , case
                          when permission = 1 and pending = 0 then 1 
                          when permission = 1 and pending = 1 then 2 
                          when permission = 0 and pending = 0 then 3 
                          when permission = 0 and pending = 1 then 4 
                       end as lookupval
                from OrigData
           ) AS O
            pivot 
            (
                max(lookupval) for [Group] in (' + @cols + ')
            ) AS P'

execute(@query)
;

Open in new window

which can be seen/used at: http://sqlfiddle.com/#!3/dc920/41
{+ 2 edits, forgot to include issue #1, refined query slightly}
0
 
LVL 1

Author Comment

by:proginc
ID: 39284053
This is much better than my own query, i don't need the deviceID in my result so this would work for me. I am trying to modify this query to get a 0 instead of null. Thanks for your help.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39284057
good luck with that zero, had me stumped. tried isnull() in a variety of ways.
please let me know if you succeed (and how)?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39284216
I have found the secret to this isnull() ... back soon
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39284259
being changed
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39284284
DEVICENAME	CUSTOMERS	SPECIALACCESS	USERS	VENDORS
MainOffice	1		0		2		4
Office1		2		0		0		0
Office2		0		0		0		1
Office3		2		4		2		3

Open in new window

here it is. simple when you think about it. the dynamically produce list of columns cannot be the same in the upper and lower portions of the pivot query. the select list must do the ISNULL() (of course!), but a different string is required for the pivot itself (which should be ordered). so, the amended code
DECLARE @cols   AS NVARCHAR(MAX)
DECLARE @incols AS NVARCHAR(MAX)
DECLARE @query  AS NVARCHAR(MAX)

-- formulate the select list with isnull(...,0) as [...] , this may not need to be ordered
SET @cols =   STUFF((SELECT distinct ', isnull(' + QUOTENAME([Group]) + ',0) as ' + QUOTENAME([Group])  AS COLORD
              FROM OrigData
              ORDER BY COLORD
              FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')

-- formulate the corresponding for [...] in, this needs to be ordered
SET @incols = STUFF((SELECT distinct ',' + QUOTENAME([Group]) AS COLORD
              FROM OrigData
              ORDER BY COLORD
              FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')

-- formulate the dynamic pivot query, using @cols and @incols
set @query = 'SELECT DeviceName, ' + @cols + ' from 
            (
                select
                      DeviceName
                    , [Group]
                    , case
                          when permission = 1 and pending = 0 then 1 
                          when permission = 1 and pending = 1 then 2 
                          when permission = 0 and pending = 0 then 3 
                          when permission = 0 and pending = 1 then 4 
                       end as lookupval
                from OrigData
           ) AS O
            pivot 
            (
                max(lookupval) for [Group] in (' + @incols + ')
            ) AS P'

-- perform the formulated query
execute(@query)
;

Open in new window

see this at: http://sqlfiddle.com/#!3/ff39e/1
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

729 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