如何在SQL中对windows函数使用筛选条件



来源:

AccNo   Amt     Date    
1       0       2/02/2021
1       200     2/02/2021
1       300     3/02/2021
2       400     1/03/2021
2       500     1/04/2021

预期目标:

AccNo   Amt     Date        no_of_times_non_0_past_week no_of_times_non_0_past_month    
1       0       2/02/2021   0                           0       
1       200     2/02/2021   0                           0       
1       300     3/02/2021   1                           1       
2       400     1/03/2021   0                           2       
2       500     1/04/2021   0                           1   

需要检查过去一周和过去一个月内金额为0的次数,依此类推。

我目前正在使用windows函数来聚合范围(过去一周、过去一个月等(。count(amt(over(按acc分区按日期排序前7行和当前行之间的范围(

现在检查我们是否可以添加一个数量为非零的过滤器,这样我就可以根据这个过滤器进行计数了。

除了windows功能之外,我们还能使用过滤器吗?count(amt(over(按acc分区按日期排序前7行和当前行之间的范围(其中amt<gt;0??

count(case when amt>0 then 1 end) over (partition by accNo order by Date rows between 7 preceding and current row)

应该为你表演把戏。

最新更新