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
 alternative of pivot query

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2013-02-01 : 14:09:05
I have a query similar to below


select type, A, B, C, D, E
from
(select type, organisation
from table1) as r
pivot
(
select sum(price)
for organisation in ([A], [B], [C], [D], [E])
) as sumofpivot




The values of column "organisation" keeps changing and so I have to again and again modify the code to include the new columns.

Is there anyway to add new columns automatically based on the values of column organisation and keep updating ?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-02-01 : 16:48:36
You would need to use a dynamic solution. Search this site for dynamic cross tab or dynamic pivot.
Here's one example:
http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-02 : 03:10:41
see

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -