【问题标题】:MySQL query for adding/subtracting values and group by month用于添加/减去值和按月分组的 MySQL 查询
【发布时间】:2021-02-03 20:37:53
【问题描述】:

我有 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, 5, 101, '2020-10-10');

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

INSERT INTO `salary_earning` VALUES
(3, 100, 20, 15, 10, 5, 30, 5, 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,
 `emp_id` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
 `date` date 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, 30, '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/

【问题讨论】:

    标签: mysql join aggregate-functions


    【解决方案1】:

    解决方案很简单(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
    

    【讨论】:

      【解决方案2】:

      你需要把日期格式做到这一点,你可以多看here

      示例:

      • 使用 %m 获取月份的数值。例如:01 作为一月

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

      【讨论】:

      • 非常感谢...以及将那个月的所有条目 w.r.t emp_id 分组为 1...现在有 2 个条目用于同一个月的员工 101
      • 太复杂了,需要对每一列进行分组求和,可以看here ref by [选择所有元素都符合条件的分组](stackoverflow.com/questions/12102200/…)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多