如何将查询返回的行转换为列?

时间:2012-08-01 07:49:12

标签: oracle oracle11g pivot

  

可能重复:
  Oracle SQL pivot query

我有以下查询,我想交换此查询的结果,以便我的所需输出格式为第一列(year_)是不同的,并显示为具有profit_sum的列标题(例如profit_sum-2006)。因为我不希望每个loan_economic和branchCode重复多年。

select to_char(ut.lcupaydt,'yyyy') year_,
       ut.lnminortp || '-' || crd.conmntyp loan_economic,
       ut.abrnchcod branchcode,
       nvl(sum(ut.lcupfamnt), 0) profit_sum

  from lcundtake ut, lcpcredit crd
 where ut.abrnchcod = crd.abrnchcod
   and ut.lnminortp = crd.lnminortp
   and ut.cfcifno = crd.cfcifno
   and ut.crserial = crd.crserial      
   AND ut.abrnchcod = 1010
   AND crd.acurrcode = 'IRR'
 group by to_char(ut.lcupaydt,'yyyy'),
          ut.lnminortp,
          ut.abrnchcod,
          ut.lnminortp || '-' || crd.conmntyp,
          crd.acurrcode
 order by to_char(ut.lcupaydt,'yyyy'), ut.lnminortp

此查询的结果是:

YEAR_   LOAN_ECONOMIC   BRANCHCODE  PROFIT_SUM
2006    31-5                1010    0
2006    35-7                1010    20789048
2006    35-8                1010    15347220
2007    10-8                1010    0
2007    11-1                1010    89760616438
2007    11-3                1010    4514005479
2007    11-4                1010    57518493151
2007    11-7                1010    6049315
2007    11-8                1010    18018260274
2007    14-1                1010    35587890413
2007    19-7                1010    154411
2007    20-8                1010    10317312500
........

我希望结果会像这样显示(想要的结果):

branchcode   loan_economic   profit_sum for2006   profit_sum for2007  profit_sum for2008... 
1010             31-5           0                   0                  null
1010             35-7        20789048                2511912822       4068344490
... ... ... ... ... ...

请帮帮我。 非常感谢

0 个答案:

没有答案