navid sedigh
navid sedigh

Reputation: 281

Add a sequence column in a query

I have a query like below

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');

and the output is: output

but I want to change the query that give me the output like below: output2

Upvotes: 0

Views: 272

Answers (1)

Dypso
Dypso

Reputation: 563

I suspect that you are looking about numbering the rows based on the rate so use an analytic function like this :

 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')
        );

Upvotes: 2

Related Questions