MySQL查询添加/减去值并按月分组

问题描述

我有 3 个表:2个表和1个sql视图。我没有得到正确的结果。

1张桌子

CREATE TABLE `salary_earning` (
  `id` int NOT NULL AUTO_INCREMENT,`basic_salary` int NOT NULL,`health_allowance` int NOT NULL,`transport_allowance` int NOT NULL,`overtime_allowance` int NOT NULL,`leave_encashment` int NOT NULL,`accomodation_allowance` int NOT NULL,`bonus_allowance` int NOT NULL,`emp_id` varchar(60) CHaraCTER SET utf8 COLLATE utf8_general_ci NOT NULL,`date` date NOT NULL,PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 

INSERT INTO `salary_earning` VALUES
(1,100,20,15,10,5,30,101,'2020-10-10');

INSERT INTO `salary_earning` VALUES
(2,'2020-10-11');

INSERT INTO `salary_earning` VALUES
(3,102,'2020-11-10');

2张桌子

CREATE TABLE `salary_deduction` (
 `id` int NOT NULL AUTO_INCREMENT,`income_tax` int NOT NULL,`advance_money` int NOT NULL,PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `salary_deduction` VALUES
(1,12,'101','2020-10-10');

3个视图

SELECT 
    slr.*,`total_earning` - `total_deduction` AS `final_salary`
FROM (
  SELECT 
    `se`.`emp_id`,`se`.`date`,(
        `basic_salary`+
        `health_allowance`+
        `transport_allowance`+
        `overtime_allowance`+
        `leave_encashment`+
        `accomodation_allowance`+
        `bonus_allowance`
    ) AS `total_earning`,IFNULL(`income_tax` + `advance_money`,0) AS `total_deduction`
  FROM `salary_earning` `se`
  LEFT JOIN `salary_deduction` `sd` ON 
    `se`.`emp_id` = `sd`.`emp_id` AND `se`.`date` = `sd`.`date`
) slr;

当前输出

+--------+------------+---------------+-----------------+--------------+
| emp_id |       date | total_earning | total_deduction | final_salary |
+--------+------------+---------------+-----------------+--------------+
|    101 | 2020-10-10 |           185 |              42 |          143 |
|    101 | 2020-10-11 |            30 |               0 |           30 |
|    102 | 2020-11-10 |           185 |               0 |          185 |
+--------+------------+---------------+-----------------+--------------+

期望的输出/我正在寻找的目标

+--------+------------+---------------+-----------------+--------------+
| emp_id |       date | total_earning | total_deduction | final_salary |
+--------+------------+---------------+-----------------+--------------+
|    101 | Oct 20     |           215 |              42 |          173 |
|    102 | Nov 20     |           185 |               0 |          185 |
+--------+------------+---------------+-----------------+--------------+

因此,如果要给新值(收入或扣除项下)添加相同的emp_id,我希望获得一个月的汇总。您能帮我做错什么吗?

https://sqlize.online/

解决方法

enter image description here 您需要输入日期格式来做到这一点,您可以查看更多here

示例:

  • 使用%m 获取月份的值数。例如: 01年1月

  • 使用%M 获取月份的值名称。例如:一月

,

解决方案非常简单(SQLize.online):

您只需要按如下格式设置date字段即可聚合数据:

SELECT -- select aggregate data
    `slr`.`emp_id`,`slr`.`date`,SUM(`total_earning`) `total_earning`,SUM(`total_deduction`) `total_deduction`,SUM(`total_earning` - `total_deduction`) AS `final_salary`
FROM (
  SELECT 
    `se`.`emp_id`,DATE_FORMAT(`se`.`date`,'%M %y') date,-- format date as month - year
    (
        `basic_salary`+
        `health_allowance`+
        `transport_allowance`+
        `overtime_allowance`+
        `leave_encashment`+
        `accomodation_allowance`+
        `bonus_allowance`
    ) AS `total_earning`,IFNULL(`income_tax` + `advance_money`,0) AS `total_deduction`
  FROM `salary_earning` `se`
  LEFT JOIN `salary_deduction` `sd` ON 
    `se`.`emp_id` = `sd`.`emp_id` AND `se`.`date` = `sd`.`date`
) slr
GROUP BY `slr`.`emp_id`,`slr`.`date`; -- group by emp_id and formatted date