I have a table like this:
| Date | Week |
|---|---|
| 2021-01-01 | 53 |
| 2021-01-02 | 53 |
| 2021-01-03 | 53 |
| 2021-01-04 | 1 |
| 2021-01-05 | 1 |
| 2021-01-06 | 1 |
| 2021-01-07 | 1 |
| ... | ... |
| 2021-12-30 | 52 |
| 2021-12-31 | 52 |
I want to rank weeks not with their values but with Date ascending order. I tried to use
dense_rank() over (order by Week)
and got this results:
| Date | Week |
|---|---|
| 2021-01-01 | 53 |
| 2021-01-02 | 53 |
| 2021-01-03 | 53 |
| 2021-01-04 | 1 |
| 2021-01-05 | 1 |
| 2021-01-06 | 1 |
| 2021-01-07 | 1 |
| ... | ... |
| 2021-12-30 | 52 |
| 2021-12-31 | 52 |
But 53rd week is on 53rd rank, not 1st as I want. Do you know what I need to use in that case? Thx
CodePudding user response:
You can try to use MOD function in ORDER BY.
Because the Week Number seem like between 1 to 53, MOD function will calculate
- MOD(53, 53)=> 0
- MOD(1, 53) => 1
so on .... .
dense_rank() over (order by MOD(Week, 53))
CodePudding user response:
use order by desc
select *, row_number()over(order by week desc) from table_name
