我如何在 sql 上加起来,这样它就不会加倍,因为 group by
Posted
技术标签:
【中文标题】我如何在 sql 上加起来,这样它就不会加倍,因为 group by【英文标题】:how do i add up on sql so it doesn't double because group by 【发布时间】:2019-11-12 02:44:30 【问题描述】:当我运行我的程序时,数字的结果反而使数字合并并将数字变成许多结果。
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;
结果是这样的 enter image description here
【问题讨论】:
【参考方案1】:您似乎正在尝试根据po number
获取sum()
。让我们把它放在一个subquery
然后join
再次那些你想要信息显示的表格。
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;
【讨论】:
但未找到列 t.idsupplier @abdulkadiralmasyhur,现在试试以上是关于我如何在 sql 上加起来,这样它就不会加倍,因为 group by的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Visual Studio 2012 中使一个类完全静态,这样它就不会出现任何错误? - C# [关闭]