If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Pivot one more than one column...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-09-11, 04:45
RogerClerkwell RogerClerkwell is offline
Registered User
 
Join Date: Jun 2011
Posts: 9
Red face Pivot one more than one column...

Stuck on trying to pivot on four columns, I have code that works one one column but can't get it to work for more than one... any ideas much apreciated.


Here's my code which works for one column:

DECLARE @query VARCHAR(4000)
DECLARE @prodtype VARCHAR(2000)
SELECT @prodtype = STUFF(( SELECT DISTINCT
'],[' + ltrim(([Product Type]))
FROM ProductCosts
ORDER BY '],[' + ltrim(([Product Type]))
FOR XML PATH('')
), 1, 2, '') + ']'

SET @query =

'SELECT * FROM
(
SELECT [ProductID], [Product Type], [Product Value]
FROM ProductCosts
)t

PIVOT (SUM([Product Value]) FOR [Product Type]
IN ('+@prodtype+')) AS pvt1
'

EXECUTE (@query)
Attached Thumbnails
Pivot one more than one column...-test_pivot.jpg  
Reply With Quote
  #2 (permalink)  
Old 12-09-11, 13:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Pivot operations are best handled on the app server (for N-tier applications) or the client (on 2-tier applications).

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On