Skip to content
Advertisement

how do i add up on sql so it doesn’t double because group by

when i’m running my program, the results of the number instead make a number merge and make the number into many results.

SELECT pengiriman_supply.po_nomor
    , data_supplier.nama_supplier
    , data_barang.nama_barang,
((sum(pengiriman_supply.jumlah))-(ifnull(masuk.terima,0))) as total 
FROM pengiriman_supply INNER JOIN data_supplier ON pengiriman_supply.idsupplier = data_supplier.id_supplier 
INNER JOIN data_barang ON pengiriman_supply.idbarang = data_barang.idbarang 
left JOIN masuk on masuk.refrence = pengiriman_supply.po_nomor 
where pengiriman_supply.tanggal between date_sub(curdate(), interval 120 day) and curdate() 
group by pengiriman_supply.po_nomor
ORDER BY pengiriman_supply.po_nomor desc;

the results is this enter image description here

Advertisement

Answer

Seems you are trying to get the sum() per po number. lets put this in a subquery then join again those tables you want info to show.

select t.po_nomor
    , p.nama_supplier
    , p1.nama_barangfrom
    , t.total
from 
    (select t1.po_nomor         
            , (sum(t1.jumlah)-coalesce(t4.terima,0)) as total 
        from pengiriman_supply t1
        inner join data_supplier t2 on t1.idsupplier = t2.id_supplier 
        inner join data_barang t3 on t1.idbarang = t3.idbarang 
        left join masuk t4 on t4.refrence = t1.po_nomor 
        where t1.tanggal between date_sub(curdate(), interval 120 day) and curdate() 
        group by t1.po_nomor) t
inner join pengiriman_supply s on s.po_nomor = t.po_nomor
inner join data_supplier p on s.idsupplier = p.id_supplier 
inner join data_barang p1 on t.idbarang = p1.idbarang 
order by t.po_nomor desc;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement