dbTalk Databases Forums  

pivot table without aggregation?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss pivot table without aggregation? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mike
 
Posts: n/a

Default pivot table without aggregation? - 03-29-2007 , 11:04 AM






Hello, I have a resultset as follows:

fields: Name, RankID

values:
Prorduct A, 4
Product B, 33
Product C, 221
(etc)

Name is always unique. RankID may not be.

I want to take that result set and basically pivot it to have the Name
values as columns, and the RankID of each one as the data. So you
would end up with only one row like:

Product A | Product B | Product C | etc
4 | 33 | 221 | etc

Is this possible? I do not want to sum the data or anything, simply
rotate it sort of.

Any advice is appreciated.


Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: pivot table without aggregation? - 03-29-2007 , 11:30 AM






Something like this should do it:

SELECT SUM(CASE WHEN [Name] = 'Product A' THEN RankID ELSE 0 END) AS
'Product A',
SUM(CASE WHEN [Name] = 'Product B' THEN RankID ELSE 0 END) AS
'Product B',
SUM(CASE WHEN [Name] = 'Product C' THEN RankID ELSE 0 END) AS
'Product C'
FROM PRODUCTS

Note that since your product name is unique the SUM here is just to pull the
data into one row. You could use MAX too but then if there is negative rank
you may have to handle it differently.

If this list of products is very dynamic, then you should look into dynamic
pivoting. Here are a few resources if you want to look in that direction:
http://www.sqlmag.com/articles/index...rticleid=15608
http://www.sqlmag.com/articles/index...rticleid=94268
http://www.sqlteam.com/item.asp?ItemID=2955

Also, most reporting tools have very good support for pivoting.

HTH,

Plamen Ratchev
http://www.SQLStudio.com



Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: pivot table without aggregation? - 03-29-2007 , 08:11 PM



Plamen Ratchev wrote:

Quote:
SELECT SUM(CASE WHEN [Name] = 'Product A' THEN RankID ELSE 0 END) AS
'Product A',
SUM(CASE WHEN [Name] = 'Product B' THEN RankID ELSE 0 END) AS
'Product B',
SUM(CASE WHEN [Name] = 'Product C' THEN RankID ELSE 0 END) AS
'Product C'
FROM PRODUCTS
I think SQL Server 2005 has a built-in pivoting function, but I've
never used it so I don't know the syntax.


Reply With Quote
  #4  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: pivot table without aggregation? - 03-29-2007 , 09:31 PM



"Ed Murphy" <emurphy42 (AT) socal (DOT) rr.com> wrote

Quote:
I think SQL Server 2005 has a built-in pivoting function, but I've
never used it so I don't know the syntax.
Here is how it can be done with PIVOT in SQL Server 2005:

SELECT [Product A],
[Product B],
[Product C]
FROM Products
PIVOT (SUM(RankID) FOR [Name] IN ([Product A],
[Product B],
[Product C])) AS P

I find the PIVOT syntax to be not so intuitive (unlike UNPIVOT which is
easier to use and understand).

Plamen Ratchev
http://www.SQLStudio.com





Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.