【问题标题】:Using count(*) .. Over(*) in mysql在 mysql 中使用 count(*) .. Over(*)
【发布时间】:2020-12-08 14:39:52
【问题描述】:

我的数据如下所示,

 requestedDate         Status
2020-04-21            APPROVED
2020-04-23            APPROVED
2020-04-27            PENDING
2020-05-21            PENDING
2020-06-01            APPROVED

我想提取如下所示的报告,其中按状态和月份进行计数。

Status      StatusCount Month   MonthCount  CountTotal
APPROVED        2        APR        3          5
PENDING         1        MAY        1          5
APPROVED        1        JUN        1          5

我的 sql 如下所示,

    select distinct
    status,
    count(status) over (partition by status) as total_by_status,
    CASE
        WHEN Month(requestedDate) = 1 THEN 'JAN'
        WHEN Month(requestedDate) = 2 THEN 'FEB'
        WHEN Month(requestedDate) = 3 THEN 'MAR'
        WHEN Month(requestedDate) = 4 THEN 'APR'
        WHEN Month(requestedDate) = 5 THEN 'MAY'
        WHEN Month(requestedDate) = 6 THEN 'JUN'
        WHEN Month(requestedDate) = 7 THEN 'JUL'
        WHEN Month(requestedDate) = 8 THEN 'AUG'
        WHEN Month(requestedDate) = 9 THEN 'SEP'
        WHEN Month(requestedDate) = 10 THEN 'OCT'
        WHEN Month(requestedDate) = 11 THEN 'NOV'
        WHEN Month(requestedDate) = 12 THEN 'DEC'
    END AS myMONTH,
  count(Month(requestedDate)) over (partition by Month(requestedDate)) as total_by_month,
  count(*) over () as Totals
from Reports
where
 requestedDate between DATE_SUB(CURDATE(), INTERVAL 120 DAY)  and date(CURDATE())
order by 1;

输出看起来像,

status    total_by_status   myMONTH total_by_month  Totals
APPROVED    3                APR        3           5
APPROVED    3                JUN        1           5
PENDING     2                APR        3           5
PENDING     2                MAY        1           5

dbfiddle

【问题讨论】:

    标签: mysql sql datetime count window-functions


    【解决方案1】:

    首先,您需要一个有效的聚合查询。然后你可以在它上面使用窗口函数(在这里,你通常会计算窗口的计数总和)。

    我会这样写:

    select 
        status,
        count(*) status_count,
        date_format(requestedDate, '%b') requested_month
        sum(count(*)) over(partition by year(requestedDate), month(requestedDate)) month_count,
        sum(count(*)) over() total_count
    from reports
    where requestedDate between current_date - interval 120 day and current_date
    group by status, year(requestedDate), month(requestedDate), date_format(requestedDate, '%b') 
    

    【讨论】:

    • 我在 requested_month 之后添加了一个 ',' 但效果很好,谢谢
    【解决方案2】:

    由于它只是过去 120 天(不会发生去年同月),所以我们也可以使用 distinct 而不是 group by),如下所示:

        select distinct status, 
               count(*) over (partition by status) as total_by_status,
               date_format(requestedDate, '%b') mymonth,
               count(Month(requestedDate)) over (partition by Month(requestedDate)) as total_by_month,
               count(*) over () as total_by_month
        from reports
        where requestedDate between current_date - interval 120 day and current_date
    order by status, mymonth
    

    Demo

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2019-02-08
      • 2019-10-22
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多