重建查询以将标题置于列下

时间:2017-05-16 17:46:27

标签: sql oracle oracle11g

我有这个问题:

select trunc(production_day, 'month') month, 
       max(case when code = 'GAS_FLARE' then NET_VOL_MTD_KNM3 else  null end )) GAS_FLARE_MTD_KNM3,           
       max(case when code = 'GAS_SP_FLARE' then NET_VOL_MTD_KNM3 else  null end ) GAS_SP_FLARE_MTD_KNM3
   from STRM_DAY
      where production_day between '01-Jan-2011' and add_months(last_day(trunc(sysdate, 'month')),-2) and (to_char(production_day+1,'DD') ='01')  group by production_day 

它给出了以下结果:

Month      GAS_FLARE_MTD_KNM3   GAS_SP_FLARE_MTD_KNM3   
01-Dec-12   1504.57460845311    3404.97562223082
01-Jan-13   7326.35568776001    3316.2514079937
01-Feb-13   157.515121158819    2422.66626163469
01-Mar-13   371.979312219054    2.34594539540587

但我希望获得以下输出:

Month      Code                    Volume 
01-Dec-12  GAS_FLARE_MTD_KNM3     1504.57460845311
01-Jan-13  GAS_FLARE_MTD_KNM3     7326.35568776001    
01-Feb-13  GAS_FLARE_MTD_KNM3     157.515121158819    
01-Mar-13  GAS_FLARE_MTD_KNM3     371.979312219054 
01-Dec-12  GAS_SP_FLARE_MTD_KNM3  3404.97562223082
01-Jan-13  GAS_SP_FLARE_MTD_KNM3  3316.2514079937
01-Feb-13  GAS_SP_FLARE_MTD_KNM3  2422.66626163469
01-Mar-13  GAS_SP_FLARE_MTD_KNM3  2.34594539540587

我怎么能这样做?

感谢, 小号

1 个答案:

答案 0 :(得分:0)

将条件聚合更改为简单聚合:

select trunc(production_day, 'month') as month,
    code || '_MTD_KNM3' as code,
    max(NET_VOL_MTD_KNM3) as volume
from STRM_DAY
where production_day between '01-Jan-2011' and add_months(last_day(trunc(sysdate, 'month')), - 2)
  and (to_char(production_day+1,'DD') ='01')
group by trunc(production_day, 'month'),
    code || '_MTD_KNM3'