Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how can I make Pivot table for unlimit columns

Author  Topic 

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-14 : 21:36:52
suppose I have a table with 3 columns
-------------------------------
emp_no | salary | city |
-------------------------------
1 ... 100 ... 1
2 ... 200 ... 9
2 ... 300 ... 2
3 ... 400 ... 1
.
.
.
100 ... 1000 ... 30
--------------------------------
what is the best way to display all cities pivoted as column
like :
---------------------------------------------------------------
emp no | city1 | city2 ............. | city n|



---------
suppose I do not want to write all city in that in Query.
I want fast way to display all cities as columns..
with as minimum code as possible.
in MS Access it is very simple & easy.
thank you..

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-14 : 21:48:27
There are only two ways to do it - either a static pivot, or a dynamic pivot. (There is a third way, but that is even more code).

If you have only 30 cities, and if the cities are known in advance, I would recommend using static pivot. It is not that much code to write, and it is as simple as this:

SELECT
*
FROM
YourTable
PIVOT
( MAX(salary) FOR City IN
([1],[2],[3],[4],[5],[6],[7],[8 ],[9],[10],
[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]))P;

If you don't know the cities in advance, then you will need dynamic pivoting. See Madhivanan's page here for code and examples: http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-14 : 21:54:57
i don't know what is the 3rd way . . . but 4th way is to handle it at your front end application


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-14 : 21:58:30
I tell you I need to write a little code ! not a few... code
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-14 : 22:04:18
quote:
Originally posted by jooorj

I tell you I need to write a little code ! not a few... code



Well, if someone else is handling the front end then do it the 4th way


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-14 : 22:06:33
Oh! khtan, the third way that I had in mind was that "manual pivoting" where you do something like:
max(case when attribute = 'attribute1' then value end ) as Attribute1
....
But jooorj wanted to write as little code as possible, so I figured why include it (unless I wanted to annoy jooorg )
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-14 : 22:12:09
quote:
Originally posted by sunitabeck

Oh! khtan, the third way that I had in mind was that "manual pivoting" where you do something like:
max(case when attribute = 'attribute1' then value end ) as Attribute1
....
But jooorj wanted to write as little code as possible, so I figured why include it (unless I wanted to annoy jooorg )



oh yeah . .


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-14 : 22:19:29
hhh, thanks all
but I ask my self why microsoft does not simplify that for our, it may be done in SQL 2013 :)
Go to Top of Page

Stubert
Starting Member

10 Posts

Posted - 2011-04-18 : 14:32:02
I found this annoying too. In Access, there is a pivot table wizard. In SSMS I had to write lots of CASE, WHEN, THEN statements. Didn't know about the PIVOT function though.
Go to Top of Page
   

- Advertisement -