简体   繁体   中英

Oracle rank function issue

Iam experiencing an issue in oracle analytic functions

I want the rank in oracle to be displayed sequentialy but require a cyclic fashion.But this ranking should happen within a group. Say I have 10 groups In 10 groups each group must be ranked in till 9. If greater than 9 the rank value must start again from 1 and then end till howmuch so ever

emp id      date1               date 2                 Rank
123         13/6/2012           13/8/2021              1
123         14/2/2012           12/8/2014              2
                                                       . 
                                                       .
123         9/10/2013           12/12/2015             9
123         16/10/2013          15/10/2013             1
123         16/3/2014           15/9/2015              2

In the above example the for the group of rows of the empid 123 i have split the rank in two subgroup fashion.Sequentially from 1 to 9 is one group and for the rest of the rows the rank again starts from 1.How to achieve this in oracle rank functions.

as per suggestion from Egor Skriptunoff above:

select
  empid, date1, date2
, row_number() over(order by date1, date2)             as "rank"
, mod(row_number() over(order by date1, date2)-1, 9)+1 as "cycle_9"
from yourtable

example result

| empid |                date1 |                date2 | rn | ranked |
|-------|----------------------|----------------------|----|--------|
| 72232 | 2016-10-26T00:00:00Z | 2017-03-07T00:00:00Z |  1 |      1 |
| 04365 | 2016-11-03T00:00:00Z | 2017-07-29T00:00:00Z |  2 |      2 |
| 79203 | 2016-12-15T00:00:00Z | 2017-05-16T00:00:00Z |  3 |      3 |
| 68638 | 2016-12-18T00:00:00Z | 2017-02-08T00:00:00Z |  4 |      4 |
| 75784 | 2016-12-24T00:00:00Z | 2017-11-18T00:00:00Z |  5 |      5 |
| 72836 | 2016-12-24T00:00:00Z | 2018-09-10T00:00:00Z |  6 |      6 |
| 03679 | 2017-01-24T00:00:00Z | 2017-10-14T00:00:00Z |  7 |      7 |
| 43527 | 2017-02-12T00:00:00Z | 2017-01-15T00:00:00Z |  8 |      8 |
| 03138 | 2017-02-26T00:00:00Z | 2017-01-30T00:00:00Z |  9 |      9 |
| 89758 | 2017-03-29T00:00:00Z | 2018-04-12T00:00:00Z | 10 |      1 |
| 86377 | 2017-04-14T00:00:00Z | 2018-10-07T00:00:00Z | 11 |      2 |
| 49169 | 2017-04-28T00:00:00Z | 2017-04-21T00:00:00Z | 12 |      3 |
| 45523 | 2017-05-03T00:00:00Z | 2017-05-07T00:00:00Z | 13 |      4 |

SQL Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM