计算特定值/功能之间有多少行

问题描述

enter image description here

我希望在看到“ LOAN_CASH_IN”时重置计数

仍然尝试使用window,但是找不到解决方案。

select aux.*,count(1) over (partition by wallet_id
                      order by order_created_at asc)
from aux

解决方法

您可以使用两个级别的窗口功能:

select aux.*,row_number() over (partition by wallet_id,grp
                          order by order_created_at asc
                         )
from (select aux.*,sum(case when order_type_micro = 'LOAN_CASH_IN' then 1 else 0 end) over (partition by wallet_id order by order_created_at) grp
      from aux
     ) aux;

row_number()更适合您的需求,除非您真的非常了解为什么将count()用作累积窗口函数。