【问题标题】:How to check the Row count Trend如何检查行数趋势
【发布时间】:2021-07-15 22:39:23
【问题描述】:

我的场景:我想查看行数趋势 我想对照过去 5 天计数的平均值检查今天的记录计数,如果阈值超过 10%,那么我想显示结果

我有以下数据和表名是 AGENT 和 为插入的日期“2021 年 4 月 4 日”加载的记录数为 5 为插入日期“4/5/2021”加载的记录数为 6 并且为插入日期“4/6/2021”加载的记录数为 1

所以我的查询应该检查最新的插入日期“2021 年 4 月 6 日”,记录计数为 1,大于 10% 的阈值,即过去 2 天的平均计数为 5.5

如果 avg 的计数与过去 2 天的平均值不匹配,我希望将结果填充为 1,如果趋势匹配,则输出应为 0

请帮忙

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql count row trend


【解决方案1】:

您可以使用聚合和窗口函数。像这样的:

select (case when cnt < avg_cnt * 0.9 then 1 else 0 end) as matching_flag
from (select inserteddt, count(*) as cnt,
             avg(count(*)) over (order by inserteddt rows between 2 preceding and 1 preceding) as avg_cnt,
             row_number() over (order by inserteddt desc) as seqnum
      from t
      where inserted_dt >= current_date - interval '2 day'
      group by inserteddt
     ) i
where seqnum = 1;

众所周知,日期函数依赖于数据库,但这给出了如何做到这一点的想法。

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2012-05-10
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多