将列转换为行(ORACLE)

时间:2015-10-02 15:19:23

标签: oracle oracle11g

我正在尝试按行转换查询列。 这是我的问题:

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 

结果如下:

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

我希望结果如下:

RANGE    TOTAL    
------- -------  
RANGE1   50000    
RANGE2   75000      
RANGE3   84000

1 个答案:

答案 0 :(得分:2)

尝试:

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