添加重复的Sums MySQL连接

时间:2017-03-22 15:59:14

标签: mysql join aggregate-functions

我有一个连接多个表的查询,我在SUM中提取了一些重复的值。

我尝试过使用一些

SELECT 
    po.vendor AS 'Factory', 
    po.doc AS 'PO#', 
    po.date AS 'Date Ordered', 
    po.due_by AS 'Est. Receive Date',
    po.status AS 'PO Status', 
    po.amount AS 'PO Total Amount', 
    ROUND(SUM(IF(b.status = 'Paid In Full', b.amount, '')),2) 'Amount Paid To PO',
    ROUND(SUM(IF(b.status != 'Paid In Full',b.amount, '')),2) 'Amount Billed But Not Payed',
    SUM(IFNULL(so.amount, 0)) as 'SO Amount Cont. Cust.',
    so.doc,
    SUM(IFNULL(d.amount, 0)) as 'Deposits on SO',
    SUM(IFNULL(i.amount, 0)) as 'Amount Invoiced Cont. Cust.',
    SUM(IFNULL(i.amount_remain, 0)) as 'Amount Remaining from Customer'

FROM po 

    LEFT JOIN 
        (SELECT bill.doc, bill.amount, bill.po_fk, bill.status FROM bill GROUP BY bill.doc) b
        ON concat('Purchase Order #', po.doc) = b.po_fk


    LEFT JOIN 
        (SELECT so.doc, so.amount FROM so GROUP BY so.doc) so 
        ON concat('Sales Order #', so.doc) = po.so_fk

    LEFT JOIN  
        (SELECT deposit.doc, deposit.amount, deposit.status, deposit.so_fk FROM deposit GROUP BY deposit.doc) d
        ON concat('Sales Order #', so.doc) = d.so_fk

    LEFT JOIN 
        (SELECT invoice.doc, invoice.amount, invoice.so_fk, invoice.amount_remain FROM invoice GROUP BY invoice.doc) i
        ON concat('Sales Order #', so.doc) = i.so_fk

    GROUP BY po.doc;

以下是一些结果

+---------------------------+-----------------+-------------------+-----------------------+---------+----------------+-----------------------------+--------------------------------+
| PO#                       | PO Total Amount | Amount Paid To PO | SO Amount Cont. Cust. | doc     | Deposits on SO | Amount Invoiced Cont. Cust. | Amount Remaining from Customer |
+---------------------------+-----------------+-------------------+-----------------------+---------+----------------+-----------------------------+--------------------------------+
| 12734-I,12736-I           |        53452.55 |              0.00 |              63931.16 | SO28292 |       34715.58 |                        0.00 |                           0.00 |
| 13303-I/13304-I           |        44080.29 |          88160.58 |             209554.44 | SO19842 |      118013.08 |                   112777.22 |                           0.00 |
| 160512JB-5                |         1095.00 |              0.00 |                  0.00 | NULL    |           0.00 |                        0.00 |                           0.00 |
| 160713JB-2                |          163.00 |            163.00 |                  0.00 | NULL    |           0.00 |                        0.00 |                           0.00 |
| 161103 MYA FLYOVER        |          937.52 |            870.05 |                349.87 | SO26797 |        1099.87 |                     1099.87 |                           0.00 |
| 161228-RG Assembly Blocks |          864.00 |            864.00 |                  0.00 | NULL    |           0.00 |                        0.00 |                           0.00 |
| 1852998                   |        39207.10 |          39207.10 |              51542.57 | SO19688 |           0.00 |                    51542.57 |                           0.00 |
| 1955463                   |        39327.21 |          39327.21 |              51664.96 | SO22327 |           0.00 |                    51664.96 |                           0.00 |
| 1957583                   |        35591.61 |          35591.61 |              47235.91 | SO22414 |           0.00 |                    47235.91 |                           0.00 |
| 1988860                   |        36373.72 |          34455.52 |              48356.20 | SO23363 |           0.00 |                    48356.20 |                           0.00 |
| 2068548                   |        36284.97 |          72569.94 |             192776.28 | SO25250 |           0.00 |                    96388.14 |                           0.00 |
| 2084384                   |        54111.82 |         108223.64 |             286526.04 | SO25651 |           0.00 |                   143263.02 |                           0.00 |
| 2084510                   |        54829.71 |         109659.42 |             289017.28 | SO25658 |           0.00 |                   144508.64 |                           0.00 |
| 2219389                   |        39066.53 |              0.00 |              51693.99 | SO29601 |           0.00 |                        0.00 |                           0.00 |
| 2219407                   |           34.70 |              0.00 |                  0.00 | NULL    |           0.00 |                        0.00 |                           0.00 |

然后只是为了验证它是否正在添加值重复次数。您可以看到SO25658上的第13行显示SUM(IFNULL(so.amount, 0)) as 'SO Amount Cont. Cust.',,其值为289017.28

mysql> select * from so where doc= 'SO25658';
+--------+---------+--------+---------+----------+
| id     | doc     | status | date    | amount   |
+--------+---------+--------+---------+----------+
| 230632 | SO25658 | Billed | 10/7/16 | 72254.32 |
+--------+---------+--------+---------+----------+
1 row in set (0.00 sec)

总结一句话,我怎样才能确保从多个JOINS的查询中删除重复值?

谢谢!

0 个答案:

没有答案