0

I need an sql select statement that would transform

1
2
3
4

into 1 2 3 4

Without using PIVOT functions as I am running oracle 10 database. Searched quite a lot on the Internet but surprisingly found nothing.

Little help?

1 Answer 1

1

You can do a pivot almost as efficiently using a cross join:

select max(case when seqnum = 1 then t.col end) as col1,
       max(case when seqnum = 2 then t.col end) as col2,
       max(case when seqnum = 3 then t.col end) as col3,
       max(case when seqnum = 4 then t.col end) as col4
from (select t.*, row_number() over (order by NULL) as seqnum
      from t
     ) t
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. Do you know a solution for the same problem if the number of columns is not limited to 4? Because in my problem - there are 6 of them and i imagine that I could have come across even higher number of columns
@Dennis . . . A SQL select query has to have a fixed number columns. You can add additional clauses in the select for addition columns. But you cannot vary the number (they will be populated with NULL if there is no value). Alternatively, you could use list_agg to put them all in one column, with delimiters.
Right, I see. Thanks a lot. I'll jot this solution down, as it seems to be a trivial task.
Either Im doing something wrong or this query returns the row filled with only max values from the column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.