繁体   English   中英

如何在 SQL 中的 Windows 函数上使用过滤条件

[英]How to use a filter condition on a windows functions in SQL

来源:

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 (partition by accNo order by Date range between 7 before and current row)

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

除了windows函数之外,我们可以使用过滤器吗? count(amt) over (partition by accNo order by Date range between 7 before and current row) where amt<> 0 ??

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

应该为你做的伎俩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM