MYSQL连接2个表,两个表的数量总和来自第1个表

时间:2017-08-17 18:11:44

标签: mysql group-by left-join

我有2个mysql表。第一个表包含7个记录,第二个表包含2个记录。第二个表中的记录与第一个表中的1个记录相关。当我试图从第1和第2表采取月份明智的金额字段时,我从表1获得双倍金额,其中第2个表有2个记录。 查询如下:

    SELECT DATE_FORMAT(a.prm_po_date, '%c'),
       DATE_FORMAT(a.prm_po_date, '%M'),
       sum(a.prm_po_amount),
       sum(b.prm_inv_pay_amt)
FROM prm_po_master a
     LEFT JOIN prm_inv_payment_dtls b
        ON a.prm_po_id = b.prm_inv_po_id_xref
           AND b.prm_inv_pay_active_status = 'Y'
WHERE     a.prm_po_dept = 'Information Technology'
      AND a.prm_po_finyear = '2017-18'
      AND a.prm_po_active_status = 'Y'
GROUP BY month(a.prm_po_date)
ORDER BY month(a.prm_po_date)

第1个表格数据prm_po_master和第2个表格prm_inv_payment_dtls

    (prm_po_id, prm_po_no, prm_po_ref_no, prm_po_finyear, prm_po_bdg_xref_no, prm_po_bdg_xref_ref, prm_po_bdg_id_xref, prm_po_date, prm_po_dept, prm_po_bdg_type, prm_po_vendor, prm_po_bdg_desc, prm_po_amount, prm_po_pay_terms, prm_po_exp_desc, prm_po_remarks, pro_po_dept_stake_holder, prm_po_invoice_amt_paid, prm_po_inv_payment_statys, prm_po_add_date, prm_po_add_user, prm_po_last_mnt_date, prm_po_last_mnt_user, prm_po_active_status, prm_po_file) VALUES
    (1, 1, 'DLBIT/PO/1/2017-18', '2017-18', 7, 'DLBIT/7/2017-18', 7, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '10', 'AMC for Internet Banking Application', 200000.00, 'Monthly Equal Amount', 'First month AMC of Internet Banking', 'First Payent', 'SJ', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'uploads/pofiles/php_upload_2.rar'), 
    (2, 2, 'DLBIT/PO/2/2017-18', '2017-18', 7, 'DLBIT/7/2017-18', 7, '10-08-2017 00:00:00', 'Information Technology', 'OPEX', '10', 'AMC for Internet Banking Application', 300000.00, 'Monthly installment 2nd payment', '2nd payment of ', '2nd month payment', 'SJ', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'NIL'), 
    (3, 3, 'DLBIT/PO/3/2017-18', '2017-18', 1, 'DLBIT/1/2017-18', 1, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '1', 'AMC for Flexcube CBS Application', 1600000.00, 'Anual', 'AMC For customisation ', 'final payment', ' SJ', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'uploads/pofiles/php_upload_3.rar'), 
    (4, 4, 'DLBIT/PO/4/2017-18', '2017-18', 1, 'DLBIT/1/2017-18', 1, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '1', 'AMC for Flexcube CBS Application', 4000000.00, '2 years payment', 'Renewal of Oracle DB Lisance', 'amount for FY 17-18 and 18-19', ' Na', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'uploads/pofiles/php_upload_4.rar'), 
    (5, 5, 'DLBIT/PO/5/2017-18', '2017-18', 1, 'DLBIT/1/2017-18', 1, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '1', 'AMC for Flexcube CBS Application', 100000.00, 'Within two years', 'Renewal of Oracle DB Lisance', '2nd installment', 'Na', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'NIL'), 
    (8, 6, 'DLBIT/PO/6/2017-18', '2017-18', 8, 'DLBIT/8/2017-18', 8, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '12', 'AMC for AML Application', 200000.00, 'In 2 installments', 'AMC for AML App', '1st installment', 'TSR', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'NIL'), 
    (9, 7, 'DLBIT/PO/7/2017-18', '2017-18', 5, 'DLBIT/5/2017-18', 5, '11-08-2017 00:00:00', 'Information Technology', 'OPEX', '11', 'AMC for ATM Txn. Recon. Application', 210000.00, 'Single Payment', 'ATM Recon App', 'Finale payment', ' SJ', 0.00, 'R', '11-08-2017 00:00:00', 'Prajith', '11-08-2017 00:00:00', 'Prajith', 'Y', 'NIL')


(prm_inv_pay_id, prm_inv_pay_date, prm_inv_ref_no, prm_inv_date, prm_inv_vendor, prm_inv_id_xref, prm_inv_po_id_xref, prm_inv_bd_id_xref, prm_inv_org_amount, prm_inv_pay_amt, prm_inv_pay_sgst, prm_inv_pay_cgst, prm_inv_pay_igst, prm_inv_pay_user, prm_inv_pay_user_design, prm_inv_pay_sys_date, prm_inv_settlment_stat, prm_inv_pay_fin_year, prm_inv_pay_dept, prm_inv_pay_active_status, prm_inv_payment_dtls, prm_inv_pay_dat_lat_mnt, prm_inv_pay_last_mod_user, prm_inv_paid_bank, prm_inv_paid_bank_acct, prm_inv_paid_bank_ifsc, prm_inv_reco_by, prm_inv_reco_thru, prm_inv_tds, prm_inv_reco_by_des, prm_inv_rec_thru_des, prm_inv_pay_no) VALUES
(1, '17-08-2017 00:00:00', 'ORC/INV/123', '09-08-2017 00:00:00', '1', 1, 4, 1, 500000.00, 100000.00, 1000.00, 1000.00, 0.00, 'Prajith', 'Asst.Manager', '17-08-2017 00:00:00', 'P', '2017-18', 'Information Technology', 'Y', 'Testing', '17-08-2017 00:00:00', 'Prajith', '5', '000100100011111', 'SBIN0000001', ' Na', 'KK', 0.00, 'AGM', 'Senior Manager', 1), 
(2, '17-08-2017 00:00:00', 'ORC/INV/123', '09-08-2017 00:00:00', '1', 1, 4, 1, 500000.00, 50000.00, 500.00, 500.00, 0.00, 'Prajith', 'Asst.Manager', '17-08-2017 00:00:00', 'P', '2017-18', 'Information Technology', 'Y', 'test', '17-08-2017 00:00:00', 'Prajith', '5', '000100100011111', 'SBIN0000001', ' Na', 'KK', 0.00, 'AGM', 'Senior Manager', 2)

请帮忙。

预期的结果是,我试图从一行中的两个表中获得月份金额的总和。在上面的情况下,8月的金额总和。

使用Inner Join,我获得了以下结果

(mon, mon_name, po_amt, paid_sum) VALUES
('8', 'August', 4000000, 150000)

左连接

(mon, mon_name, po_amt, paid_sum) VALUES
('8', 'August', 2610000, NULL), 
('8', 'August', 4000000, 150000)

预期结果

(mon, mon_name, po_amt, paid_sum) VALUES
('8', 'August',6610000, 150000)

1 个答案:

答案 0 :(得分:0)

你应该加入总和以获取详细信息而不是行

rows <- 1
for (i in 64:111) {
    Prod1[1:rows, i] <- 0
    rows = rows+1
}