1

I am trying to convert my query columns in rows. This is my query:

select sum(CASE WHEN 
                     t.price < 5000
                THEN t.price END) AS RANGE1,
       sum(CASE WHEN 
                     t.price between 5000 and 10000 
                THEN t.price END) AS RANGE2,
       sum(CASE WHEN 
                     t.price > 10000
                THEN t.price END) AS RANGE3
From
      cars t
Where
      t.status = 3 

The result is like this:

RANGE1   RANGE2    RANGE3
-------  -------  --------
50000     75000      84000

I want the result is as follows:

RANGE    TOTAL    
------- -------  
RANGE1   50000    
RANGE2   75000      
RANGE3   84000
  • Here is a thread with some examples http://stackoverflow.com/questions/19858079/how-to-convert-columns-into-rows-in-oracle?lq=1. – jsh Oct 02 '15 at 15:23

1 Answers1

2

Try:

select 
        'RANGE1' RANGE,
        sum(CASE WHEN 
                     t.price < 5000
                THEN t.price END) AS total
From
      cars t
Where
      t.status = 3 

union all

select 
        'RANGE2' RANGE,
       sum(CASE WHEN 
                     t.price between 5000 and 10000 
                THEN t.price END) AS total
From
      cars t
Where
      t.status = 3 

union all

select  
        'RANGE3' RANGE,
       sum(CASE WHEN 
                     t.price > 10000
                THEN t.price END) AS total
From
      cars t
Where
      t.status = 3 
Praveen
  • 8,945
  • 4
  • 31
  • 49