I need to enumerate my lines in a column of the select by a type. For example, I have three types which can be "RR", "RJ" and "RA", every time that a type "RR" appears I have to sum 3 and the other ones I have to sum only 1, creating a sequence like:
Type Number
RR 3
RR 6
RJ 7
RR 10
RJ 11
RA 12
RR 15
I have other fields in the select, so I used the ROW_NUMBER() function with all my order by fields, something like:
select
number,
[...]
type,
ROW_NUMBER() OVER (order by number, type [...] )*3 as sequence
from
my_table
order by number, type [...]
I also tried to use a case statement, but it doesn't aggregate the values.
Is it possible to do? I'm trying to use the ROW_NUMBER()
function, but i can't get the result, only three by three.
You could use SUM
:
select
number,
[...]
type,
SUM(CASE WHEN Type = 'RR' THEN 3 ELSE 1 END)
OVER (order by number, type [...] ) as sequence
from my_table
order by number, type [...]