SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 alternative of pivot query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

230 Posts

Posted - 02/01/2013 :  14:09:05  Show Profile  Reply with Quote
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 ?

Edited by - learning_grsql on 02/01/2013 14:10:13

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 02/01/2013 :  16:48:36  Show Profile  Reply with Quote
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

India
52323 Posts

Posted - 02/02/2013 :  03:10:41  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000