在查询中添加序列列

时间:2018-11-17 08:57:40

标签: sql oracle oracle-sqldeveloper

我有一个类似下面的查询

select ref_leger_code,rate,sum(balance),to_char(due_date,'yyyymm')
from tbl_value_temp
group by ref_leger_code,rate,to_char(due_date,'yyyymm');

,输出为: output

但是我想更改查询,使我的输出如下所示: output2

1 个答案:

答案 0 :(得分:2)

我怀疑您正在考虑根据rate对行进行编号,因此请使用如下分析函数:

 select ref_leger_code, rate, sumbalance, due_date,
        ROW_NUMBER() OVER (PARTITION BY rate ORDER BY due_date asc ) AS sequence  
 from   (
          select ref_leger_code, rate, sum(balance) sumbalance, to_char(due_date,'yyyymm') due_date
          from   tbl_value_temp
          group by ref_leger_code, rate, to_char(due_date,'yyyymm')
        );