MySQL 每年每季度计数,计数为 0

MySQL Count per Quarter per Year, with 0 count

我有一个 table 有行李,列:

status(VARCHAR) - ("lost", "found" 等)

找到日期(日期) - (YYYY-MM-DD)


我的行李table:

   -------------------------------------------
  | status | otherattributes |   datefound    |
   -------------------------------------------
  | lost   | ............... |   2014-11-17   |
  | found  | ............... |   2015-05-28   |
  | lost   | ............... |   2016-11-17   |
  | lost   | ............... |   2015-10-20   |
                     etc..

我想每季度、每年 table 计算状态为“丢失”的行李件数。 同时 returning 没有“lost”件的宿舍(计数 = 0)。

我想要的:

所需的 table 看起来像这样:

   ------------------------------
  | year | quarter | amountlost |
   ------------------------------
  | 2014 |    1    |     23     |
  | 2014 |    2    |     41     |
  | 2014 |    3    |      0     |
  | 2014 |    4    |     12     |
  | 2015 |    1    |     32     |
  | 2015 |    2    |      0     |
  | 2015 |    3    |      9     |
  | 2015 |    4    |     27     |
  | 2016 |    1    |     53     |
  | 2016 |    2    |     24     |
  | 2016 |    3    |     11     |
  | 2016 |    4    |      0     |
   ------------------------------

我现在拥有的:

我目前有一个查询,但它没有 return 年 + 季度且 COUNT 为 0。我尝试使用临时 tables 但我无法得到它工作..

我正在处理的当前查询:

(没有给出想要的结果)

SELECT YEAR(datefound) AS year, QUARTER(datefound) AS quarter, COUNT(status) AS amountlost FROM luggage WHERE status = 'lost' GROUP BY YEAR(datefound), QUARTER(datefound) ORDER BY YEAR(datefound), QUARTER(datefound)

结果(不需要):

   ------------------------------
  | year | quarter | amountlost |
   ------------------------------
  | 2014 |    4    |     10     |
  | 2015 |    1    |     32     |
  | 2015 |    2    |      0     |
  | 2015 |    3    |      9     |
  | 2015 |    4    |     27     |
  | 2016 |    1    |     53     |
   ------------------------------

以上结果 table 缺少 2014 年和 2016 年的季度,这将导致 0 计数 @ amountlost。

希望有人能帮我解决一个查询(也许是临时 tables?),为我提供所需的 table!

在这种情况下,您有两种选择。当您的输入数据包含所有年份和季度,但 where 子句将它们过滤掉时,情况会更容易。然后你可以切换到条件聚合:

SELECT YEAR(datefound) AS year, 
       QUARTER(datefound) AS quarter, 
       SUM(status = 'lost') AS amountlost
FROM luggage
GROUP BY YEAR(datefound), QUARTER(datefound)
ORDER BY YEAR(datefound), QUARTER(datefound);

如果这不起作用,则您需要生成可能的行,然后添加附加信息。假设数据中每年和每个季度都有代表:

SELECT y.yyyy, q.qq, COUNT(l.status) as AmountLost
FROM (SELECT DISTINCT YEAR(datefound) as yyyy FROM luggage) y CROSS JOIN
     (SELECT DISTINCT QUARTER(datefound) as qq FROM LUGGAGE) q LEFT JOIN
     luggage l
     ON YEAR(l.datefound) = y.yyyy AND QUARTER(l.datefound) = q.qq AND
        l.status = 'lost'
GROUP BY y.yyyy, q.qq;

如果您的数据在这方面甚至不完整,那么您需要生成所需的行。类似于:

SELECT yq.yyyy, yq.qq, COUNT(l.status) as AmountLost
FROM (SELECT 2014 as yyyy, 1 as qq  UNION ALL
      SELECT 2014, 2 UNION ALL
      . . .
     ) yq LEFT JOIN
     luggage l
     ON YEAR(l.datefound) = yq.yyyy AND QUARTER(l.datefound) = yq.qq AND
        l.status = 'lost'
GROUP BY yq.yyyy, yq.qq;

注意:您可能已经有一个 table,其中包含适合报告的年份和季度。如果是这样,您可以使用它。如果你有某种数字 table,也可以使用它(带有一些额外的逻辑)。