SQL - 如何在一年中的每个日期按年龄和状态对项目进行分组/计数 - 第 2 部分

Posted

技术标签:

【中文标题】SQL - 如何在一年中的每个日期按年龄和状态对项目进行分组/计数 - 第 2 部分【英文标题】:SQL - How to group/count items by age and status on every date of a year - part 2 【发布时间】:2020-07-09 20:50:25 【问题描述】:

这个问题类似于我在这里提出并回答的另一个问题:SQL - How to group/count items by age and status on every date of a year?,但我无法弄清楚这个新问题。我需要查询帮助,以便将下面示例表中的数据分组到下面的所需结果中。目标是在从指定日期开始到当前日期结束的每个连续日期结束时,按组汇总给定状态的票数。

示例数据表(门票):

ticket_id | opened    | assigned  | in_work  | closed     | assigned_group
---------------------------------------------------------------------------
 1          1/1/2020    1/2/2020    1/5/2020   1/5/2020     Network
 2          1/2/2020    1/3/2020    1/3/2020   1/5/2020     Software
 3          1/2/2020    1/5/2020                            Hardware
 4          1/2/2020                                        Network
 5          1/3/2020    1/4/2020    1/5/2020                Software
 6          1/3/2020                                        Network
... and more continuing in similar pattern

期望的结果:

Date      | assigned_group | num_open | num_assigned | num_in_work | num_closed |
---------------------------------------------------------------------------
 1/1/2020   Network          1            0              0             0 
 1/1/2020   Software         0            0              0             0 
 1/1/2020   Hardware         0            0              0             0 
 1/2/2020   Network          1            1              0             0 
 1/2/2020   Software         1            0              0             0 
 1/2/2020   Hardware         1            0              0             0 
 1/3/2020   Network          2            1              0             0 
 1/3/2020   Software         1            0              1             0 
 1/3/2020   Hardware         1            0              0             0  
 1/4/2020   Network          2            1              0             0 
 1/4/2020   Software         0            1              1             0 
 1/4/2020   Hardware         1            0              0             0 
 1/5/2020   Network          2            0              0             1 
 1/5/2020   Software         0            0              1             1 
 1/5/2020   Hardware         0            1              0             0  
... continuing to present date

谢谢!

【问题讨论】:

只是好奇:2020 年 1 月 6 日会发生什么?票 1 和票 2 于 2020 年 1 月 5 日关闭,您希望它们仍算作 num_in_work? 好的,阅读 Gordon 的回答,给我一个答案:) 【参考方案1】:

您可以取消透视并使用累积和:

with da as (
      select opened as date, assigned_group, 1 as open_inc, 0 as assigned_inc, 0 as in_work_inc, 0 as closed_inc
      from t
      union all
      select assigned, assigned_group, -1, 1, 0, 0
      from t
      union all
      select in_work, assigned_group, 0 -1, 1, 0
      from t
      union all
      select closed, assigned_group, 0, 0, -1, 1
      from t
     )
select date, assigned_group,
       sum(sum(open_inc)) over (partition by assigned_group order by date) as num_opens,
       sum(sum(assigned_inc)) over (partition by assigned_group order by date) as num_assigned,
       sum(sum(in_work_inc)) over (partition by assigned_group order by date) as num_in_work,
       sum(sum(closed_inc)) over (partition by assigned_group order by date) as num_closed
from da
group by date, assigned_group
order by date, assigned_group;

【讨论】:

以上是关于SQL - 如何在一年中的每个日期按年龄和状态对项目进行分组/计数 - 第 2 部分的主要内容,如果未能解决你的问题,请参考以下文章

SQL计算年龄

日期与php的差异

如何在 Looker 中按日期计算状态变化?

sql mysql

SQL语句如何根据出生日期计算年龄

Oracle PL/SQL,如何使用简单的替换变量和months_between计算出生日期的年龄