带有默认未来月份的财务年度数据值mysql查询

时间:2018-07-30 04:39:29

标签: mysql join

我正在生成2018-2019财政年度(4月至3月)的图表,为此,我正在获取每个月的平均销售总额。我想获取整个财政年度的数据,即使该数据无法用于将来的月份,因为在图形中我使用了组合条形图,因此这是其中的一部分,其他将类似地添加。使用下面的查询,我只获得数据库中插入数据的月份的平均总数,但是我需要如下所示的所有财务年度数据。

必需的响应-

"sales" : [
    {
        "yearMonth" : "2018-04",
        "netTotal" : "154800.0"
    },
    {
        "yearMonth" : "2018-05",
        "netTotal" : "450410.0"
    },
    {
        "yearMonth" : "2018-06",
        "netTotal" : "124780.0"
    },
    {
        "yearMonth" : "2018-07",
        "netTotal" : "98750.0"
    },
    {
        "yearMonth" : "2018-08",
        "netTotal" : "0.0"
    },
    {
        "yearMonth" : "2018-09",
        "netTotal" : "0.0"
    },
    and so on till (March) 31-03-2019.....
]

查询1-

SELECT DATE_FORMAT(quotationDate, '%Y-%m') AS yearMonth, AVG(netTotal) as netTotal FROM
quotation_master WHERE companyId = '1' AND fiscalYear = '2018-2019' GROUP BY yearMonth

查询2-

SELECT meses.month, CONCAT(YEAR(CURDATE()), '-', meses.month) as yearMonth, IF(AVG(netTotal) IS NULL, 0, AVG(netTotal)) as netTotal FROM(
        SELECT 01 AS MONTH
        UNION SELECT 02 AS MONTH
        UNION SELECT 03 AS MONTH
        UNION SELECT 04 AS MONTH
        UNION SELECT 05 AS MONTH
        UNION SELECT 06 AS MONTH
        UNION SELECT 07 AS MONTH
        UNION SELECT 08 AS MONTH
        UNION SELECT 09 AS MONTH
        UNION SELECT 10 AS MONTH
        UNION SELECT 11 AS MONTH
        UNION SELECT 12 AS MONTH
) as meses
LEFT JOIN quotation_master QM ON meses.month = MONTH(quotationDate) 
WHERE QM.companyId = '1' AND QM.fiscalYear = '2018-2019' GROUP BY yearMonth

查询输出- 由于可以获取到7月的数据,因此仅提供该数据,而没有其他月份(如8月,9月,10月等)的数据。我知道未来几个月数据库中不存在数据,这就是为什么我希望将0.0作为未来几个月的默认值。

"sales" : [
        {
            "yearMonth" : "2018-04",
            "netTotal" : "154800.0"
        },
        {
            "yearMonth" : "2018-05",
            "netTotal" : "450410.0"
        },
        {
            "yearMonth" : "2018-06",
            "netTotal" : "124780.0"
        },
        {
            "yearMonth" : "2018-07",
            "netTotal" : "98750.0"
        }
    ]

2 个答案:

答案 0 :(得分:2)

如上所述,问题在您的WHERE标准之内。 由于您的WHERE将条件应用于初始FROM表并筛选出返回的数据集。

要解决此问题,只需按WHERE的条件将AND替换为JOIN ON,以限制仅合并表数据的过滤数据集。

示例:http://sqlfiddle.com/#!9/3181f0/2

表架构和数据:

 CREATE TABLE quotation_master 
        (`companyId` int, `quotationDate` datetime, `netTotal` int, `fiscalYear` varchar(10))
    ;

INSERT INTO quotation_master 
    (`companyId`, `quotationDate`, `netTotal`, `fiscalYear`)
VALUES
    (1, '2018-04-01 00:00:00', 154800.0, '2018-2019'),
    (1, '2018-04-02 00:00:00', 154800.0, '2018-2019'),
    (1, '2018-05-01 00:00:00', 450410.0, '2018-2019'),
    (1, '2018-05-02 00:00:00', 450410.0, '2018-2019'),
    (1, '2018-06-01 00:00:00', 124780.0, '2018-2019'),
    (1, '2018-06-02 00:00:00', 124780.0, '2018-2019'),
    (1, '2018-07-01 00:00:00', 98750.0, '2018-2019'),
    (1, '2018-07-02 00:00:00', 98750.0, '2018-2019')
;

查询:

SELECT 
  meses.month, 
  CONCAT(YEAR(CURDATE()), '-', meses.month) as yearMonth, 
  COALESCE(AVG(netTotal), 0) as netTotal 
FROM(
        SELECT 01 AS MONTH
        UNION SELECT 02 AS MONTH
        UNION SELECT 03 AS MONTH
        UNION SELECT 04 AS MONTH
        UNION SELECT 05 AS MONTH
        UNION SELECT 06 AS MONTH
        UNION SELECT 07 AS MONTH
        UNION SELECT 08 AS MONTH
        UNION SELECT 09 AS MONTH
        UNION SELECT 10 AS MONTH
        UNION SELECT 11 AS MONTH
        UNION SELECT 12 AS MONTH
) as meses
LEFT JOIN quotation_master QM 
ON meses.month = MONTH(quotationDate) 
AND QM.companyId = '1' 
AND QM.fiscalYear = '2018-2019' 
GROUP BY yearMonth;

结果

| month | yearMonth | netTotal |
|-------|-----------|----------|
|     1 |    2018-1 |        0 |
|    10 |   2018-10 |        0 |
|    11 |   2018-11 |        0 |
|    12 |   2018-12 |        0 |
|     2 |    2018-2 |        0 |
|     3 |    2018-3 |        0 |
|     4 |    2018-4 |   154800 |
|     5 |    2018-5 |   450410 |
|     6 |    2018-6 |   124780 |
|     7 |    2018-7 |    98750 |
|     8 |    2018-8 |        0 |
|     9 |    2018-9 |        0 |

注意:如我的评论中所述,请确保测试COALESCE(AVG(netTotal), 0)而不是AVG(COALESCE(netTotal, 0))的期望结果,因为MySQL跳过了NULL聚合列并且不会考虑行数存在但为NULL

答案 1 :(得分:1)

您的问题出在WHERE子句中。由于未来几个月数据库中没有数据,因此QM.companyId = '1' AND QM.fiscalYear = '2018-2019'的测试将失败,因为这两个值均为NULL。尝试将WHERE子句更改为

WHERE QM.companyId = '1' AND QM.fiscalYear = '2018-2019' OR QM.companyId IS NULL