views:

1311

answers:

4

I'm using reporting services to make a report graph. However my data looks like this:

Table1
    C01    C02   C03   C04
    1      2     3     4

I need to do a sql query to return data that looks like this:
    Any_Col_name
    1
    2
    3
    4

I'm using MS Reporting Services with a Oracle DB. I cannot restructure the table.

+1  A: 

Have a look here.

http://support.microsoft.com/kb/175574

It describes how to "rotate" a table in SQL Server. I know you said Oracle buy you might glean something from it.

Iain M Norman
That's the wrong direction. it's already flat and he wants it vertical.
@MB righto :) Ta.
Iain M Norman
I found another answer of yours to mod +1... no hard feelings.
+5  A: 
select c01 from table
union all
select c02 from table
union all
select c03 from table
union all
select c04 from table
Why was this voted down?
Sam Saffron
Why is Mark being downvoted? A union is the correct way to handle this with generic ANSI SQL.
cdonner
I think Pax did it out of rage ...
Sam Saffron
This was probably voted down due to the comment flame war.
chilltemp
And flagged as offensive... what a dick
I didn't vote it down, because it works, hence is helpful. Efficiency is another matter. I *did* flag it offensive because of the language Mark used in his comments - I make no apologies for that.
paxdiablo
Free and frank, huh? You're not flagging me as offensive, nor the comment, you're flagging the post, where there's nothing offensive. but I'm not surprised.
That's the way I work. My view is that people should be punished for offensive language and that's the only way to do it for offense in comments. Take it up with the site owners and they can reverse it. If they also tell me not to do it again, I'll abide by that. Or raise a uservoice request.
paxdiablo
It's not offensive language, it offended you. Commenting on someone's language skills isn't offensive to the public at large, nor is priggish or snob. Not to mention, there's no "punishment" but to remove the post, a correct answer as you yourself point out. So you think that *I* will be "punished"
+2  A: 

If you are using Oracle 11G and above, you can also use unpivot for this, it should be more efficient than the union all (haven't tested this cause I do not have oracle around)

SELECT Any_Col_name  FROM table   
    UNPIVOT INCLUDE NULLS (Any_Col_name FOR Col IN (C01,C02,C03,C04))
Sam Saffron
He didn't say the Oracle was 11g. Unpivot isn't available until then, unless MSRS will just restructure the query sent to Oracle to gather every row and then apply the SQL Server syntax to that result set. Not sure how that will work.
@Mark, I edited the answer to reflect. MSRS will not do anything funky to the queries.
Sam Saffron
Kewl... Always a good idea to give the latest and greatest as an option... tease people with what they could have if only.
This is the best answer as long as UNPIVOT is supported.
achinda99
A: 

We have solved this problem many times. Your best plan of action is to write appropriate PL/SQL functions that will iterate through all of the columns, outputting them as rows. I say to do it this way, because this will probably not be the last time you use this functionality.

AFHood