Sudha Eswaran
Sudha Eswaran

Reputation: 3

SQL Convert Rows to Columns without aggreagate or pivot

I am trying to convert rows to columns . Please look at the below sample:

colname  |  data1 |   data2

---------|--------|------------------------------------
     a   |    1   |
     a   |    2   |
     a   |    3   |
     b   |    4   |
     b   |    5   |
     c   |        |     6  
     c   |        |     7

I want the output as:

      a  |   b     |   c

---------|--------|------------------------------------
     1   |        |
     2   |        |
     3   |        |
         |    4   |
         |    5   |
         |        |     6  
         |        |     7

Could anyone please share your ideas on how to solve this?

Upvotes: 0

Views: 128

Answers (1)

LukStorms
LukStorms

Reputation: 29677

This isn't really putting rows to columns. So it's not a PIVOT that's needed.
You just want to show values in some columns depending on what value colname has.
A CASE WHEN can be used for that.

select 
case when colname = 'a' then data1 end as a,
case when colname = 'b' then data1 end as b,
case when colname = 'c' then data2 end as c
from yourtable;

Upvotes: 2

Related Questions