In the recent project, a group ranking problem occurs as follows:
The database has the following table:
Depart a B C group
detachment 1 34.4 3.3 1
detachment 1 33.3 32.2 1
detachment 3 2 3.3 22.2 1
detachment 4 3.3 4.4 33.3 1
high-tech Zone 2.2 3.3 22.2 2
Economic Development Zone 3.3 23 33.3 2
Qujiang district 4.5 35 23.9 2
weiyang district 12.2 39 3.3 2
Chang 'an district 30.3 23 2.2 2
I want to use statements to implement the following functions. The following table is returned using statements:
Depart a B C group Total ranking
Detachment 2 1 33.3 32.2 1 66.5 1
4 detachment 3.3 4.4 33.3 1 41 2
One detachment 1 34.4 3.3 1 38.7 3
Third Detachment 2 3.3 22.2 1 27.5 4
Qujiang district 4.5 35 23.9 2 63.4 1
Economic Development Zone 3.3 23 33.3 2 59.6 2
Chang 'an district 30.3 23 2.2 2 55.5 3
Weiyang district 12.2 39 3.3 2 54.4 4
Hi-Tech Zone 2.2 3.3 22.2 2 27.7 5
solution:
select *
from
(select. *, nvl (. b, 0) + nvl (. c, 0) total score
, row_number () over (partition by group order by nvl (. b, 0) + nvl (. c, 0) ranking
from tb a
)
order by group, ranking