Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / March 2007

Tip: Looking for answers? Try searching our database.

pivot table without aggregation?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 29 Mar 2007 17:04 GMT
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.
Plamen Ratchev - 29 Mar 2007 17:30 GMT
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.cfm?articleid=15608
http://www.sqlmag.com/articles/index.cfm?articleid=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
Ed Murphy - 30 Mar 2007 02:11 GMT
> SELECT SUM(CASE WHEN [Name] = 'Product A' THEN RankID ELSE 0 END) AS
> 'Product A',
[quoted text clipped - 3 lines]
> '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.
Plamen Ratchev - 30 Mar 2007 03:31 GMT
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.