mysql - 即使数据集中不存在月份,也会从查询中返回每月数据

标签 mysql sql

我是一名 MS SQL Server 人员,但我必须编写一些 MySQL 查询。我正在尝试编写一个查询,该查询将显示选定员工的每月销售数据,如果该员工没有该月的销售数据,则显示该月份和 0。

这是我的查询,但它返回 NULL?

CREATE TABLE `saleamountbyemployee` (
  `month_year` varchar(9) DEFAULT NULL,
  `total_sales` int(11) NOT NULL,
  `employee` char(17) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `saleamountbyemployee` (`month_year`,`total_sales`,`employee`) VALUES ('Feb 18','34512','James Jones'); 

INSERT INTO `saleamountbyemployee` (`month_year`,`total_sales`,`employee`) VALUES ('Feb 18','223','Sally Smith'); 

INSERT INTO `saleamountbyemployee` (`month_year`,`total_sales`,`employee`) VALUES ('Feb 18','22','James Jones'); 

WITH RECURSIVE
cte_months_to_pull AS (
    SELECT DATE_FORMAT(@start_date, '%Y-%m-01') - INTERVAL @number_of_months MONTH AS month_to_pull
    UNION ALL
    SELECT month_to_pull + INTERVAL 1 MONTH
    FROM cte_months_to_pull
    WHERE month_to_pull < @start_date + INTERVAL @number_of_months - 2 MONTH
)
SELECT YRS.months_to_pull,T.employee,COALESCE(T.IA, 0) IA
FROM (SELECT DATE_Format(month_to_pull, '%b-%Y') months_to_pull
      FROM cte_months_to_pull
      ORDER BY months_to_pull
     ) AS YRS
LEFT JOIN (SELECT Date_format(month_year, '%b-%Y') AS `Month`
                 ,employee,Sum(total_sales) AS IA

           FROM   saleamountbyemployee
           WHERE  employee = 'James Jones'
           GROUP  BY Date_format(month_year, '%b-%Y'), employee) T
ON YRS.months_to_pull = T.`Month`
order by month(STR_TO_DATE(CONCAT('01-',months_to_pull), '%d-%b-%Y')),YEAR(STR_TO_DATE(CONCAT('01-',months_to_pull), '%d-%b-%Y'))

编辑
如果我将语法更改为:

SET @start_date = 'Jan 18';
SET @number_of_months = 12;

WITH RECURSIVE
cte_months_to_pull AS (
    SELECT str_to_date(CONCAT(@start_date,' 01'), '%b %y %d') - INTERVAL @number_of_months MONTH AS month_to_pull
    UNION ALL
    SELECT month_to_pull + INTERVAL 1 MONTH
    FROM cte_months_to_pull
    WHERE month_to_pull < @start_date + INTERVAL @number_of_months - 2 MONTH
)
SELECT YRS.months_to_pull,T.employee,COALESCE(T.IA, 0) IA
FROM (SELECT DATE_Format(month_to_pull, '%b-%Y') months_to_pull
      FROM cte_months_to_pull
      ORDER BY months_to_pull
     ) AS YRS
LEFT JOIN (SELECT Date_format(month_year, '%b-%Y') AS `Month`
                 ,employee,Sum(total_sales) AS IA

           FROM   saleamountbyemployee
           WHERE  employee = 'James Jones'
           GROUP  BY Date_format(month_year, '%b-%Y'), employee) T
ON YRS.months_to_pull = T.`Month`
order by month(STR_TO_DATE(CONCAT('01-',months_to_pull), '%d-%b-%Y')),YEAR(STR_TO_DATE(CONCAT('01-',months_to_pull), '%d-%b-%Y'))

我现在收到此错误消息:

Error Code: 1292. Incorrect datetime value: 'Jan 18'

最佳答案

CTE 中的这一行:

SELECT DATE_FORMAT(@start_date, '%Y-%m-01') - INTERVAL @number_of_months MONTH AS Month_to_pull

“%Y-%m-01”的日期格式与表中的内容不匹配。如果使用“Feb 18”作为@start_date 参数的值,它将返回 NULL。您指定的 date_format 希望它看起来像这样:

SELECT DATE_FORMAT('2018-01-01', '%Y-%m-01') - 间隔 1 个月作为month_to_pull

在日期中添加“01”,这会使其认为是该月的第一天。

SELECT str_to_date(CONCAT(@start_date,' 01'), '%b %y %d') - INTERVAL @number_of_months MONTH AS Month_to_pull

如果您使用“Feb 18”作为@start_date并使用1作为@number_of_months,这将返回以下内容:

2018年1月1日上午12:00:00

表中的 VARCHAR 数据类型和日期格式会让您烦恼。

关于mysql - 即使数据集中不存在月份,也会从查询中返回每月数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57435077/

相关文章:

mysql - 如何在 MySQL EDITED 中隐藏别名列

mysql - sequelize 多对多关系

mysql - 更改 WAMP MySQL 数据位置不起作用

mysql - 用于基于数百万个Player-> Player事件分析MMO播放器之间关系的数据库?

php - 数据库切片密码的最后两个字符(?!)

php - 根据 GMT 转换日期和时间

sql - 如何在 SQL 中搜索日期?

mysql - SQL 按一列中的多个值组分组

.net - 为什么 Entity Framework 的 DbContext.Find() 会生成带有 select top 2 的查询?

mysql - 将多行结果转换为一行 SQL