Friday, February 24, 2012

Display Row Data As Cols

I have data in rows that I need to aggregate and display in a columnar fashion and I haven't been able to figure out how to do it. A simplified version of the data as it is stored in the table:

Station Month Day Reading

1 1 1 100

1 1 2 200

1 1 3 300

1 2 1 400

1 2 2 500

And I would like to create a query that returns:

Station Month Day 1 Day 2 Day 3

1 1 100 200 300

1 2 400 500

Any help you can provide or tips to steer me in the right direction are much appreciated.

Dianne Siebold

Are you looking for something like this?

http://msdn2.microsoft.com/en-us/library/ms177410.aspx

|||That is very cool. I'm using SQL Server 2005 but I need to maintain query compatibility with Oracle so I'm looking for something that uses joins or maybe case statements to make it more portable.|||

Then maybe the responses in this thread might be helpful for you

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=438605&SiteID=1

Starting from the second reply by limno.

|||I'm working on using that query logic but I'm just not getting how I can apply it to my query. I'll let you now if I have any success. Thanks!|||I think I got it. I was able to take the concepts from the thread you referred me to and use them in another query. Thanks!

No comments:

Post a Comment