簡體   English   中英

如何檢查行數趨勢

[英]How to check the Row count Trend

我的場景:我想檢查行計數趨勢我想檢查今天的記錄計數與過去 5 天計數的平均值,如果閾值超過 10%,那么我想顯示結果

我有以下數據,表名是 AGENT,為插入的日期“4/4/2021”加載的記錄數為 5,為插入的日期“2021 年 4 月 5 日”加載的記錄數為 6,加載的記錄數對於插入日期“2021 年 4 月 6 日”是 1

所以我的查詢應該檢查最新的插入日期“2021 年 4 月 6 日”,記錄計數為 1,大於 10% 的閾值,即過去 2 天的平均計數為 5.5

如果 avg 的計數與過去 2 天的平均值不匹配,我希望結果填充為 1,那么如果趨勢匹配,output 應該為 0

請幫忙

在此處輸入圖像描述

您可以使用聚合和 window 函數。 像這樣的東西:

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;

日期 function 臭名昭著地依賴於數據庫,但這為如何做到這一點提供了一個思路。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM