SUM GROUP BY与多个表上的子查询

时间:2017-12-19 12:19:50

标签: mysql subquery

我必须为我实际开发的项目解决与MySQL查询相关的问题。

如果有可能,我想在多个表上获得MySQL结果。我试过子查询但没有成功。这是数据库结构:

CREATE TABLE t_meeting
(
meeting_n               INT AUTO_INCREMENT             PRIMARY KEY,
meeting_user_n          INT                          NOT NULL,
meeting_gremium_n       INT                          NOT NULL,
meeting_gremium_f_n     VARCHAR(100)                 NOT NULL,
meeting_pay_flat        INT                          NULL,
meeting_no_pay          INT                          NULL,
meeting_pay_methode     INT                          NULL,
meeting_pay             INT                          NULL,
meeting_date            DATE                         NULL,
meeting_date_to         DATE                         NULL,
meeting_prepare         TIME                         NULL,
meeting_duration        TIME                         NULL,
meeting_allowance       DECIMAL(9, 2)                NULL,
meeting_expences        DECIMAL(9, 2)                NULL,
meeting_ahv             INT                          NULL
)

CREATE TABLE t_meeting_pay
(
meeting_pay_n            INT AUTO_INCREMENT PRIMARY KEY,
meeting_pay_user_n       INT                          NOT NULL,
meeting_pay_date         DATE                         NULL,
meeting_pay_paiment_date DATE                         NULL,
meeting_pay_pay_for      INT                          NOT NULL,
meeting_pay_amount       DECIMAL(9, 2) DEFAULT '0.00' NULL,
meeting_pay_meeting_n    INT                          NOT NULL
)

CREATE TABLE t_user_pay
(
user_pay_n      INT AUTO_INCREMENT PRIMARY KEY,
user_pay_user_n INT           NOT NULL,
user_pay_date   DATE          NULL,
user_pay_amount DECIMAL(9, 2) NOT NULL,
user_pay_year   INT(4)        NOT NULL  #<- Container the year of the meeting_date field
)

CREATE TABLE t_gremium
(
gremium_n          INT AUTO_INCREMENT PRIMARY KEY,
gremium_name       VARCHAR(250)    NULL,
gremium_name_short VARCHAR(50)     NULL,
gremium_address    VARCHAR(250)    NULL,
gremium_zip        INT(5)          NULL,
gremium_city       VARCHAR(100)    NULL,
gremium_sector     INT             NULL,
gremium_branch     INT             NULL,
gremium_lvl        INT             NULL,
gremium_ask        INT DEFAULT '0' NULL,
gremium_ask_usern  INT             NOT NULL
)

CREATE TABLE t_pay_for
(
pay_for_n  INT AUTO_INCREMENT PRIMARY KEY,
pay_for_de VARCHAR(40) NOT NULL,
pay_for_fr VARCHAR(40) NOT NULL,
pay_for_it VARCHAR(40) NOT NULL
)

表格链接

用户拥有属于Gremium的会议:

t_meeting.meeting_user_n - &gt; t_user.user_n t_meeting.meeting_gremium_n - &gt; t_gremium.gremium_n

当付款从Gremium:

进入时,会计员工可以插入t_meeting_pay行

t_meeting_pay.meeting_pay_meeting_n - &gt; t_meeting.meeting_n

所有员工都有会议。对他们来说,他们可以获得津贴和费用。这笔钱流向了社会。从所有这些钱中,员工获得最高1000美元。 Tis金额是每年。因此,如果在7月的当年达到1000美元,社会将向员工支付1000美元。如果没有达到,在年底,员工将获得支付给社会的金额,例如600美元。

现在我想创建一个返回以下行的SQL查询:

User | Year | Payed allowance | Payed expenses | Payed to user
John | 2017 |          500.00 |         200.00 |        700.00
John | 2018 |          400.00 |         150.00 |          0.00
  

支付津贴= SUM(t_meeting_pay)WHERE t_meeting_pay_pay_for = 1 GROUP BY year(t_meeting.meeting_date)

     

支付费用= SUM(t_meeting_pay)WHERE t_meeting_pay_pay_for = 2 GROUP BY年份(t_meeting.meeting_date)

     

支付给用户= SUM(t_user_pay)GROUP BY年(t_meeting.meeting_date)

因此,最终结果必须包含来自同一个表的2个SUM,但具有按年分组的不同WHERE条件,来自另一个按年分组的SUM和用户数据。

我创建的查询:

SELECT SUM(meeting_pay_amount) AS total, YEAR(meeting_date) AS year,
(
  SELECT SUM(user_pay_amount) AS payed
  FROM t_user_pay
  WHERE user_pay_year = YEAR(meeting_date)
  GROUP BY user_pay_year
) AS total_payed,
(
  SELECT SUM(meeting_pay_amount) AS payed
  FROM t_meeting_pay
  WHERE (1) AND meeting_pay_pay_for = 1
  GROUP BY YEAR(meeting_date)
) AS total_payed_a,
(
  SELECT SUM(meeting_pay_amount) AS payed
  FROM t_meeting_pay
  WHERE (1) AND meeting_pay_pay_for = 2
  GROUP BY YEAR(meeting_date)
) AS total_payed_e, user_lname
FROM t_meeting_pay
LEFT JOIN t_meeting ON t_meeting_pay.meeting_pay_meeting_n = t_meeting.meeting_n
LEFT JOIN t_user ON t_meeting.meeting_user_n = t_user.user_n
LEFT JOIN t_user_pay ON t_user.user_n = t_user_pay.user_pay_user_n
LEFT JOIN t_gremium ON t_meeting.meeting_gremium_n = t_gremium.gremium_n
LEFT JOIN t_pay_for ON t_meeting_pay.meeting_pay_pay_for = t_pay_for.pay_for_n
WHERE meeting_pay_user_n = 1
GROUP BY year

我希望有人可以帮助我。此查询使用total_payed_a和total_payed_e在所有行中具有相同值的唯一错误...

提前感谢您的回复。

编辑:

SQLFiddle link

现在正在努力工作但结果应该是:

total | year | total_payed | total_payed_a | total_payed_e | user_lname
1600  | 2017 |      500.00 |        600.00 |        300.00 |        Dee
 800  | 2018 |      300.00 |       1000.00 |        500.00 |        Dee

1 个答案:

答案 0 :(得分:1)

经过大量工作后,我得到了一个返回预期结果的查询。

New SQLFiddle

查询:

    SELECT user_ma, user_lname, user_fname,
    YEAR(meeting_date) AS meeting_year,
    (
    SELECT SUM(user_pay_amount) AS payed
    FROM t_user_pay
    WHERE user_pay_year = YEAR(meeting_date)
    GROUP BY user_pay_year
    ) AS total_payed_to_user,
    (
    SELECT SUM(meeting_pay_amount)
    FROM t_meeting_pay
    LEFT JOIN t_meeting ON t_meeting_pay.meeting_pay_meeting_n = t_meeting.meeting_n
    WHERE meeting_pay_pay_for = '1' AND YEAR(meeting_date) = meeting_year
    ) AS total_payed_a,
    (
    SELECT SUM(meeting_pay_amount)
    FROM t_meeting_pay
    LEFT JOIN t_meeting ON t_meeting_pay.meeting_pay_meeting_n = t_meeting.meeting_n
    WHERE meeting_pay_pay_for = '2' AND YEAR(meeting_date) = meeting_year
    ) AS total_payed_e,
    (
    SELECT SUM(meeting_pay_amount)
    FROM t_meeting_pay
    LEFT JOIN t_meeting ON t_meeting_pay.meeting_pay_meeting_n = t_meeting.meeting_n
    WHERE  YEAR(meeting_date) = meeting_year
    ) AS total_payed
    FROM t_meeting_pay
    LEFT JOIN t_meeting ON t_meeting_pay.meeting_pay_meeting_n = t_meeting.meeting_n
    LEFT JOIN t_user ON t_meeting.meeting_user_n = t_user.user_n
    LEFT JOIN t_user_pay ON t_user.user_n = t_user_pay.user_pay_user_n
    LEFT JOIN t_gremium ON t_meeting.meeting_gremium_n = t_gremium.gremium_n
    LEFT JOIN t_pay_for ON t_meeting_pay.meeting_pay_pay_for = t_pay_for.pay_for_n
    WHERE meeting_pay_user_n = 1
    GROUP BY meeting_year

有人可以告诉我是否可以提高此查询的效率?

谢谢大家的意见。

伊夫