Creating a rank that resets on a specific value of a column

橙三吉。 提交于 2019-12-05 15:07:18

One approach is to use a cumulative conditional sum to identify the groups and then use row_number() for the ranking:

select t.*,
       row_number() over (partition by CustomerNumber, grp
                          order by date
                         ) as rank
from (select t.*,
             sum(case when channel = 'A' then 1 else 0 end) over
                 (partition by CustomerNumber order by date) as grp
      from t
     ) t;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!