?
Solved

Problem with dynamic pivot table

Posted on 2011-05-11
4
Medium Priority
?
290 Views
Last Modified: 2012-08-14
I am having problems with the following dynamic pivot table generation.  It is not sorting the columns correctly in the output.  Can someone help with why my code is not working?

 result.csv
DECLARE @Columns varchar(8000)
select @columns = isnull(@columns+',','') + '['+[STZAE]+']' from DRAP_ECO_1 group by [STZAE]

exec ('SELECT * 

From

   (SELECT [DOKNR], [STZAE] as EventCode, [DATUM] as EventDate from DRAP_ECO_1
    union
    SELECT [DOKNR], [STZAE] as EventCode, [DATUM] as EventDate from DRAP_ECO_1) as src

PIVOT

   (max(eventdate) FOR Eventcode IN ('+@columns+') )AS pvt

order by 1,2')

Open in new window

0
Comment
Question by:Fairfield
4 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35742580
>> It is not sorting the columns correctly in the output.

It sorted on first column as mentioned in the query. What sort order are you looking for?
0
 
LVL 4

Expert Comment

by:musalman
ID: 35745262
Are you looking for Sorting on Date ???

Then Use Order By 3
0
 

Author Comment

by:Fairfield
ID: 35747040
I am wanting to order by column name [STZAE].
0
 
LVL 22

Accepted Solution

by:
Nico Bontenbal earned 2000 total points
ID: 35747125
Try changing
select @columns = isnull(@columns+',','') + '['+[STZAE]+']' from DRAP_ECO_1 group by [STZAE]
to
select @columns = isnull(@columns+',','') + '['+[STZAE]+']' from DRAP_ECO_1 group by [STZAE] order by [STZAE]
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question