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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Pivot Cross Table with data dependant colums

Author  Topic 

djorre
Yak Posting Veteran

94 Posts

Posted - 2009-06-11 : 08:24:02
Hi Friends,

In order to create reports out of an event logging, I need to crosstab a log-table. The parameter names and values that are logged (with a certain timestamp) in the logtable then appair in the report in colums (grouped per timestamp), and the timestamps become unique rows.

The problem is, that there can be a parameter added or deleted. So I can not make it with select max case, bevause then the number of colums is fixed. and the pivot function syntax is as I found

quote:
SELECT *
FROM #temp123
PIVOT
(
SUM(VaribleValue)
FOR [Variable] IN ([Sales],[Expenses],[Taxes],[Profit])
)
AS p



So with hard coded Colums. It this possible to say

quote:
SELECT *
FROM #temp123
PIVOT
(
SUM(VaribleValue)
FOR [Variable] IN (select parameters from logtable group by parameters)
)
AS p



Does that work I could not test it yet? Or any suggestions ?

Thank in advance,

Joris

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-11 : 08:33:54
Only with dynamic sql.

See http://www.sommarskog.se/dynamic_sql.html


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

djorre
Yak Posting Veteran

94 Posts

Posted - 2009-06-11 : 08:46:54
Thanks! A new world has been opened to me : )
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-11 : 11:25:41
also see

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page
   

- Advertisement -