获取直到上一个日期的待处理票证积压计数

Get Pending tickets backlog count until previous date

我有table这样

Ticket Date  |Total Tickets Created | Pending Tickets 
------------------------------------------------------
01-04-2021   |5000                  | 200 
02-04-2021   |2000                  | 100 
03-04-2021   |3000                  | 300 
   select t.created_date,t.source,t.ticket_type,
   count(*) as Total_Tickets_Created,
   sum(case when tch.before='Pending Credential' then 1 else 0 end) as Pending_Tickets,
        from r4e_mongo.mongo_repbiz_tickets t
        join r4e_mongo.mongo_repbiz_ticket_changelog tch on t.id=cast(tch.ticket_id as string) 

我想获取直到前一天的待定工单积压,如下所示,以便在 google 数据工作室中创建带有日期过滤器的报告。

Ticket Date  |Total Tickets Created | Pending Tickets | Pending Tickets Backlog
---------------------------------------------------------------------------------
01-04-2021   |5000                  | 200             |    1000
02-04-2021   |2000                  | 100             |    1100 --(1000+100)
03-04-2021   |3000                  | 300             |    1400 --(1100+300)

例如...我在报告中选择了 10 月 15 日...它应该显示 10 月 14 日之前的未决工单积压计数

尝试 sum 作为 window 函数:

with mytable as (
  select date '2021-04-01' as ticket_date, 5000 as total, 200 as pending union all
  select date '2021-04-02', 2000, 100 union all  
  select date '2021-04-03', 3000, 300
)
select 
  *,
  sum(pending) over (order by ticket_date) as backlog
from mytable