您好我想问一下 sum 查询,我在执行查询时遇到了困惑。
我想计算总电流。
来料总和
select (select sum(jumlah) from warehouse where tgl_invoice >= '2013-03-17' AND
tgl_invoice <='2013-03;18' and kode=a.kode) total,a.kode,a.nama from warehouse a
group by a.kode;
结果 :
+--------+----------+------------+ | 总计| 科德 | 名 | +--------+----------+------------+ | 4 | ACLG001 | AC LG 1 包 | | 180 | P001 | 主轴 | | 40 | S012 | 电缆 | +--------+----------+------------+
SUM材料(货物)使用
select (select sum(jumlah) from use_material where tanggal >= '2013-03-17' AND
tanggal <='2013-03;18' and kode_barang=a.kode) total,a.kode,a.nama from warehouse a
group by a.kode;
结果 :
+--------+----------+------------+ | 总计| 科德 | 名 | +--------+----------+------------+ | 空 | ACLG001 | AC LG 1 包 | | 20 | P001 | 主轴 | | 10 | S012 | 电缆 | +--------+----------+------------+
现在我想对传入的材料(货物)减去材料(货物)的使用进行求和
select
(select sum(jumlah) from warehouse
where tgl_invoice >= '2013-03-17' AND tgl_invoice <='2013-03;18' and kode=a.kode) -
(select sum(jumlah) from use_material
where tanggal >= '2013-03-17' AND tanggal <='2013-03;18' and kode_barang=a.kode)
total,a.kode,a.nama from warehouse a group by a.kode;
结果 :
+--------+----------+------------+ | 总计| 科德 | 名 | +--------+----------+------------+ | 空 | ACLG001 | AC LG 1 包 | | 160 | P001 | 主轴 | | 30 | S012 | 电缆 | +--------+----------+------------+
这里是计算错误。AC LG 1 PK 是否必须为 4。但结果为 NULL
请回答。