MySQL UNION ALL(完全加入)与条件聚合

时间:2017-10-18 07:41:23

标签: mysql aggregate outer-join

我有三张桌子:

CREATE TABLE `Agreement` (
  `AID` bigint(20) NOT NULL AUTO_INCREMENT,
  `FLAGS` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`AID`)
);

CREATE TABLE `Assessment` (
  `ASMID` bigint(20) NOT NULL AUTO_INCREMENT,
  `AID` bigint(20) NOT NULL DEFAULT '0',
  `Amount` decimal(19,4) NOT NULL DEFAULT '0.0000',
  `Description` text,
  PRIMARY KEY (`ASMID`)
);

CREATE TABLE `Payment` (
  `RID` bigint(20) NOT NULL AUTO_INCREMENT,
  `AID` bigint(20) NOT NULL DEFAULT '0',
  `ASMID` bigint(20) NOT NULL DEFAULT '0',
  `Amount` decimal(19,4) NOT NULL DEFAULT '0.0000',
  `Description` text,
  PRIMARY KEY (`RID`)
);

我正在插入一份协议,三份评估,五份付款行,如下所示:

INSERT INTO Agreement(FLAGS) VALUES(0);
INSERT INTO Assessment(AID, Amount, Description) VALUES (1, 1200, "Rent");
INSERT INTO Assessment(AID, Amount, Description) VALUES (1, 20, "Damage - car break");
INSERT INTO Assessment(AID, Amount, Description) VALUES (1, 500, "Damage - vehicle");
INSERT INTO Payment(AID, ASMID, Amount, Description) VALUES(1, 1, 500, "Rent Fee");
INSERT INTO Payment(AID, ASMID, Amount, Description) VALUES(1, 1, 600, "Rent Fee");
INSERT INTO Payment(AID, ASMID, Amount, Description) VALUES(1, 2, 20, "Damage Fee");
INSERT INTO Payment(AID, Amount, Description) VALUES(1, 600, "Deposit Fee");
INSERT INTO Payment(AID, Amount, Description) VALUES(1, 50, "Application Fee");

当我看到数据时,它应该如下所示:

mysql> SELECT * FROM Agreement;
+-----+-------+
| AID | FLAGS |
+-----+-------+
|   1 |     0 |
+-----+-------+
1 row in set (0.00 sec)

mysql> SELECT * FROM Assessment;
+-------+-----+-----------+--------------------+
| ASMID | AID | Amount    | Description        |
+-------+-----+-----------+--------------------+
|     1 |   1 | 1200.0000 | Rent               |
|     2 |   1 |   20.0000 | Damage - car break |
|     3 |   1 |  500.0000 | Damage - vehicle   |
+-------+-----+-----------+--------------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM Payment;
+-----+-----+-------+----------+-----------------+
| RID | AID | ASMID | Amount   | Description     |
+-----+-----+-------+----------+-----------------+
|   1 |   1 |     1 | 500.0000 | Rent Fee        |
|   2 |   1 |     1 | 600.0000 | Rent Fee        |
|   3 |   1 |     2 |  20.0000 | Damage Fee      |
|   4 |   1 |     0 | 600.0000 | Deposit Fee     |
|   5 |   1 |     0 |  50.0000 | Application Fee |
+-----+-----+-------+----------+-----------------+
5 rows in set (0.00 sec)

因此,任何协议都需要在不久的将来支付多次评估。它可能有多个可能与评估相关的付款(即租金)或可能不是(即申请费)。

现在,实际上,有多个协议有多个评估和多个付款。

现在我希望结果涵盖与Assessment GROUPED BY第一个协议,第二个评估相关联的表PaymentAgreement中的所有行。此外,我需要从表PaymentsApplied表中为每个评估汇总AMOUNT Payment,以便我们可以将其与表Amount中的Assessment进行比较{{1} }。此外,如果任何付款与任何评估无关,则不进行汇总。结果如下:

AmountDue

我尽力解释情况。实际上,在我的应用程序中,查询已连接10个表,如协议!

欢迎任何帮助!!

更新1

我已从此查询开始,

+-----+-------+-----------+-----------------+--------------------+-----------------+
| AID | ASMID | AmountDue | PaymentsApplied |          ASM-Descr | PMT-Description |
+-----+-------+-----------+-----------------+--------------------+-----------------+
|   1 |     1 | 1200.0000 |       1100.0000 |               Rent |        Rent Fee |
|   1 |     2 |   20.0000 |         20.0000 | Damage - car break |     Damange Fee |
|   1 |     3 |  500.0000 |            NULL |   Damage - vehicle |            NULL |
|   1 |     0 |      NULL |        600.0000 |               NULL |     Deposit Fee |
|   1 |     0 |      NULL |         50.0000 |               NULL | Application Fee |
+-----+-------+-----------+-----------------+--------------------+-----------------+
5 Rows

给了我结果,

(SELECT DISTINCT
    Payment.RID, Payment.Amount as PaymentsApplied, Payment.ASMID as PMT_ASMID, null as AmountDue, null AS ASMID
FROM Payment
    LEFT JOIN Assessment ON Assessment.ASMID=Payment.ASMID)
UNION
(SELECT DISTINCT
    null, null, null, Assessment.Amount, Assessment.ASMID
FROM Assessment
    LEFT JOIN Payment ON Payment.ASMID=Assessment.ASMID)
ORDER BY ASMID, PMT_ASMID;

现在,从这一点来看,IDK如何通过评估ID(ASMID)汇总付款行并与+------+-----------------+-----------+-----------+-------+ | RID | PaymentsApplied | PMT_ASMID | AmountDue | ASMID | +------+-----------------+-----------+-----------+-------+ | NULL | NULL | NULL | 1200.0000 | 1 | | NULL | NULL | NULL | 20.0000 | 2 | | NULL | NULL | NULL | 500.0000 | 3 | | 1 | 500.0000 | 1 | NULL | NULL | | 2 | 600.0000 | 1 | NULL | NULL | | 3 | 20.0000 | 2 | NULL | NULL | | 4 | 600.0000 | 0 | NULL | NULL | | 5 | 50.0000 | 0 | NULL | NULL | +------+-----------------+-----------+-----------+-------+ 8 rows in set (0.01 sec) 表一起加入?

更新2

我已经制作了sqlfiddle link以防万一有人想尝试一下。

我在查询中添加了条件聚合,

Agreement

给了我,

(SELECT DISTINCT
    null as AmountDue,
    null AS ASMID,
    null as ASM_Descr,
    Payment.Description as PMT_Descr,
    (CASE WHEN Payment.ASMID > 0 THEN SUM(Payment.Amount) ELSE Payment.Amount END) as PaymentsApplied,
    (CASE WHEN Payment.ASMID > 0 THEN GROUP_CONCAT(Payment.RID) ELSE Payment.RID END) as PaymentList,
    Payment.ASMID as PMT_ASMID
FROM Payment
    LEFT JOIN Assessment ON Assessment.ASMID=Payment.ASMID
    GROUP BY Assessment.ASMID)
UNION ALL
(SELECT DISTINCT
    Assessment.Amount,
    Assessment.ASMID,
    Assessment.Description,
    null,
    null,
    null,
    null
FROM Assessment
    LEFT JOIN Payment ON Payment.ASMID=Assessment.ASMID
    GROUP BY Assessment.ASMID)
ORDER BY ASMID, PMT_ASMID;

但是,这一行仍然缺少付款行(RID:5)中的一行,而且我没有得到预期的结果。

2 个答案:

答案 0 :(得分:1)

您似乎想要每个AID + ASMID + PMT-Description的结果行。所以:

  1. 从付款中选择并汇总。
  2. 从评估中选择。
  3. 全外加入两者,因为可以在没有付款的情况下进行没有访问和访问的付款。
  4. MySQL缺少FULL OUTER JOIN。所以写两次相同的查询,一次使用LEFT OUTER JOIN,一次使用RIGHT OUTER JOIN,并在两个结果集上使用UNION

    select
      p.aid,
      p.asmid,
      a.amount as amount_due,
      p.payments_applied,
      a.description as asm_description,
      p.description as pmt_description
    from
    (
      select aid, asmid, description, sum(amount) as payments_applied
      from payment
      group by aid, asmid, description
    ) p
    left join assessment a on a.aid = p.aid and a.asmid = p.asmid
    union
    select
      p.aid,
      p.asmid,
      a.amount as amount_due,
      p.payments_applied,
      a.description as asm_description,
      p.description as pmt_description
    from
    (
      select aid, asmid, description, sum(amount) as payments_applied
      from payment
      group by aid, asmid, description
    ) p
    right join assessment a on a.aid = p.aid and a.asmid = p.asmid
    order by aid, asmid, pmt_description;
    

    一旦MySQL功能FULL OUTER JOIN,您可以将此查询减半。

答案 1 :(得分:1)

我首先收集所有评估,然后将他们加入付款,然后在没有评估的情况下结合所有付款:

# Assessments with payments

SELECT asm.AID,
       asm.ASMID,
       min(asm.Amount) AS AmountDue,
       SUM(pam.Amount) AS PaymentsApplied,
       asm.Description AS `ASM-Descr`,
       pam.Description AS `PMT-Descr`,
       agr.FLAGS
FROM Assessment asm
LEFT JOIN Payment pam ON pam.ASMID = asm.ASMID
JOIN Agreement agr ON agr.AID = asm.AID
GROUP BY asm.AID,
         asm.ASMID
UNION # Payments without assessments

SELECT pam.AID,
       pam.ASMID,
       NULL AS AmountDue,
       SUM(pam.Amount) AS PaymentsApplied,
       NULL AS `ASM-Descr`,
       pam.Description AS `PMT-Descr`,
       agr.FLAGS
FROM Payment pam
LEFT JOIN Assessment asm ON pam.ASMID = asm.ASMID
JOIN Agreement agr ON agr.AID = pam.AID
WHERE asm.ASMID IS NULL
GROUP BY pam.AID, pam.RID;

如果要添加更多信息,可以包装此结果,为其命名并将更多表连接到临时结果:

SELECT payment_overview.*,
       p.name
FROM
    ( # Assessments with payments
 SELECT asm.AID,
        asm.ASMID,
        min(asm.Amount) AS AmountDue,
        SUM(pam.Amount) AS PaymentsApplied,
        asm.Description AS `ASM-Descr`,
        pam.Description AS `PMT-Descr`,
        agr.FLAGS
     FROM Assessment asm
     LEFT JOIN Payment pam ON pam.ASMID = asm.ASMID
     JOIN Agreement agr ON agr.AID = asm.AID
     GROUP BY asm.AID,
              asm.ASMID
     UNION # Payments without assessments
 SELECT pam.AID,
        pam.ASMID,
        NULL AS AmountDue,
        SUM(pam.Amount) AS PaymentsApplied,
        NULL AS `ASM-Descr`,
        pam.Description AS `PMT-Descr`,
        agr.FLAGS
     FROM Payment pam
     LEFT JOIN Assessment asm ON pam.ASMID = asm.ASMID
     JOIN Agreement agr ON agr.AID = pam.AID
     WHERE asm.ASMID IS NULL
     GROUP BY pam.AID,
              pam.RID ) AS payment_overview
JOIN Payor p ON p.AID = payment_overview.AID ;