没有结果日期范围时选择零

时间:2018-01-05 16:08:13

标签: mysql

我有一个查询,我用它来取回前6个月数据的每月总费用。我需要解决的问题是,当特定月份没有记录时,不会返回任何内容,只显示5个月。

我需要修改此查询以始终显示6个月,即使特定月份没有数据但我不确定如何完成此操作。

        select sum(cost),
            CASE
                WHEN MONTH(collection_date) = 1 THEN 'January'
                WHEN MONTH(collection_date) = 2 THEN 'February'
                WHEN MONTH(collection_date) = 3 THEN 'March'
                WHEN MONTH(collection_date) = 4 THEN 'April'
                WHEN MONTH(collection_date) = 5 THEN 'May'
                WHEN MONTH(collection_date) = 6 THEN 'June'
                WHEN MONTH(collection_date) = 7 THEN 'July'
                WHEN MONTH(collection_date) = 8 THEN 'August'
                WHEN MONTH(collection_date) = 9 THEN 'September'
                WHEN MONTH(collection_date) = 10 THEN 'October'
                WHEN MONTH(collection_date) = 11 THEN 'November'
                WHEN MONTH(collection_date) = 12 THEN 'December'
                ELSE 'NULL'
                END AS datemodified
            from invoices
            WHERE collection_date >= DATE_SUB(now(), INTERVAL 5 MONTH)
            GROUP BY MONTH(collection_date)
            ORDER BY collection_date asc;

空月的结果示例

 COST      Datemodified      
 300       September
 200       November
 200       December

期望的输出

 COST      Datemodified
 0         August
 300       September
 0         October
 200       November
 200       December

1 个答案:

答案 0 :(得分:2)

您可以创建假月数据并将发票表加入其中。试试这个:

SELECT SUM(cost), months.name AS datemodified
FROM (SELECT 1 AS num, 'January' AS name
      UNION SELECT 2, 'February'
      UNION SELECT 3, 'March'
      UNION SELECT 4, 'April'
      UNION SELECT 5, 'May'
      UNION SELECT 6, 'June'
      UNION SELECT 7, 'July'
      UNION SELECT 8, 'August'
      UNION SELECT 9, 'September'
      UNION SELECT 10, 'October'
      UNION SELECT 11, 'November'
      UNION SELECT 12, 'December') months
LEFT JOIN invoices.collection_date = months.num
WHERE collection_date >= DATE_SUB(NOW(), INTERVAL 5 MONTH)
GROUP BY MONTH(collection_date)
ORDER BY collection_date ASC;

然而,这给了你所有12个月。要仅获得最近6个月,您需要动态生成假月数据:

SELECT SUM(cost),
       CASE num WHEN 1 THEN 'January'
                WHEN 2 THEN 'February'
                WHEN 3 THEN 'March'
                WHEN 4 THEN 'April'
                WHEN 5 THEN 'May'
                WHEN 6 THEN 'June'
                WHEN 7 THEN 'July'
                WHEN 8 THEN 'August'
                WHEN 9 THEN 'September'
                WHEN 10 THEN 'October'
                WHEN 11 THEN 'November'
                WHEN 12 THEN 'December'
       END AS datemodified
FROM (SELECT MONTH(NOW()) AS num
      UNION SELECT MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AS num
      UNION SELECT MONTH(DATE_SUB(NOW(), INTERVAL 2 MONTH)) AS num
      UNION SELECT MONTH(DATE_SUB(NOW(), INTERVAL 3 MONTH)) AS num
      UNION SELECT MONTH(DATE_SUB(NOW(), INTERVAL 4 MONTH)) AS num
      UNION SELECT MONTH(DATE_SUB(NOW(), INTERVAL 5 MONTH)) AS num) months
LEFT JOIN invoices.collection_date = months.num
WHERE collection_date >= DATE_SUB(NOW(), INTERVAL 5 MONTH)
GROUP BY MONTH(collection_date)
ORDER BY collection_date ASC;