通过UNION计算,合并查询重复代码

时间:2017-12-08 13:48:53

标签: mysql

我正在努力解决这个问题,主要是因为重复可能会减少 - 希望如此。

一般的想法是显示给定月份的每个表的总和,并将其显示为数据透视表。

目前的代码在我看来非常混乱和重复,但主要的是它不允许我进行计算,因为UNION加入。

对于每个月如下(我假设这必须是一个衍生表,它取另一个的值并计算每个月的这个公式):

Calculation Formula = (
    employees_salaries + 
    employees_salaries_insurence_costs +
    employees_agreement +
    employees_agreement_insurence_cost + 
    employees_extras +
    employees_to_hand +
    employees_delegations ) - employees_deductions

当前查询正在正常显示所有内容,但问题和所需的输出:

  • 问题:是否有任何可能的查询缩小           - 启用计算公式的可能的查询结构

所需的输出:当前的布局是需要保留的

我正在尝试几天,但我无法达到我想要的结果,任何提示都会非常有用。

输出

+------------------------------------+---------------------+------+-------+-------+-----+------+-------+----------+-----------+---------+----------+----------+
|         employees_salaries         |       January       | Febr | March | April | May | June | July  |  August  | September | October | November | December |
+------------------------------------+---------------------+------+-------+-------+-----+------+-------+----------+-----------+---------+----------+----------+
| employees_salaries                 | 0                   |    0 |     0 |     0 |   0 |    0 |     0 | 15826.12 |         0 |       0 |        0 |        0 |
| Calculations                       | Calculation Formula |      |       |       |     |      |       |          |           |         |          |          |
| employees_salaries_insurence_costs | 0                   |    0 |     0 |     0 |   0 |    0 |     0 |  5044.44 |         0 |       0 |        0 |        0 |
| employees_agreement                | 0                   |    0 |     0 |     0 |   0 |    0 |   250 |        0 |         0 |       0 |        0 |     3550 |
| employees_agreement_insurence_cost | 0                   |    0 |     0 |     0 |   0 |    0 | 71.05 |        0 |         0 |       0 |        0 |   178.65 |
| employees_extras                   | 0                   |    0 |     0 |     0 |   0 |    0 |     0 |        0 |         0 |    1.00 |        0 |        0 |
| employees_deductions               | 0                   |    0 |     0 |     0 |   0 |    0 |     0 |    45.60 |         0 |    1.00 |        0 |        0 |
| employees_to_hand                  | 0                   |    0 |     0 |     0 |   0 |    0 |     0 |        0 |         0 |  111.00 |   555.00 |        0 |
| employees_delegations              | 0                   |    0 |     0 |     0 |   0 |    0 |     0 |        0 |      2520 |     540 |        0 |        0 |
+------------------------------------+---------------------+------+-------+-------+-----+------+-------+----------+-----------+---------+----------+----------+

MCVE:https://www.db-fiddle.com/f/8RvfD3uA4cSSVgmhoP2co6/9

0 个答案:

没有答案