Skip to content
Advertisement

Rank based on cumulative value

I want to rank on ID and value columns based on ascending order of UID. Expected output has to change once value column has a different value than the previous value. Ranks has to restart on each new ID

Here is a sample dataset that I have created:

Advertisement

Answer

I think that the simplest approach to this gaps-and-islands problem is to use lag() to retrieve the “previous” value, and then a window sum that increments everytime the value changes.

Demo on DB Fiddle:

uid | id | value | grp
--: | -: | ----: | --:
  1 |  1 |     0 |   1
  2 |  1 |     0 |   1
  3 |  1 |     1 |   2
  4 |  1 |     1 |   2
  5 |  1 |     1 |   2
  6 |  1 |     0 |   3
  7 |  1 |     1 |   4
  8 |  1 |     0 |   5
  9 |  1 |     0 |   5
 10 |  1 |     0 |   5
 11 |  2 |     1 |   1
 12 |  2 |     1 |   1
 13 |  2 |     0 |   2
 14 |  2 |     0 |   2
 15 |  2 |     1 |   3
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement