我手边有个特殊的问题。我需要按以下方式排列:
)。

如何制定一个查询?
发布于 2021-05-21 20:25:48
你需要两个步骤:
select
id_col
,dt_col
,dense_rank()
over (order by min_dt, id_col, dt_col - rnk) as part_col
from
(
select
id_col
,dt_col
,min(dt_col)
over (partition by id_col) as min_dt
,rank()
over (partition by id_col
order by dt_col) as rnk
from tab
) as dtdt_col - rnk计算出相同的结果,对于连续日期,->相同的等级
发布于 2021-05-21 19:43:52
尝试引导/滞后的datediff,然后执行分区排序。
select t.ID_COL,t.dt_col,
rank() over(partition by t.ID_COL, t.date_diff order by t.dt_col desc) as rankk
from ( SELECT ID_COL,dt_col,
DATEDIFF(day, Lag(dt_col, 1) OVER(ORDER BY dt_col),dt_col) as date_diff FROM table1 ) t发布于 2021-05-21 22:00:53
考虑这个问题的一种方法是“什么时候增加1级”。当具有相同id_col的行上的前一个值相差超过一天时,就会发生这种情况。或者行是标识最早的一天。
这就将这个问题转化为一个累积的总和:
select t.*,
sum(case when prev_dt_col = dt_col - 1 then 0 else 1
end) over
(order by min_dt_col, id_col, dt_col) as ranking
from (select t.*,
lag(dt_col) over (partition by id_col order by dt_col) as prev_dt_col,
min(dt_col) over (partition by id_col) as min_dt_col
from t
) t;https://stackoverflow.com/questions/67642753
复制相似问题