0

在我的系统中计算了来料和用料后,如果有人要进行调整,则有一个调整项。

QUERY 来料 - 材料的使用

select (select sum(jumlah) from warehouse where tgl_invoice >= '2013-03-17' AND tgl_invoice <='2013-03;18' and kode=a.kode) - COALESCE((select sum(jumlah) from use_material where tanggal >= '2013-03-17' AND tanggal <='2013-03;18' and kode_barang=a.kode),0)  total, a.kode, a.nama from warehouse a group by a.kode;
+--------+----------+------------+
| 总计| 科德 | 名 |
+--------+----------+------------+
| 4 | ACLG001 | AC LG 1 包 |
| 160 | P001 | 主轴 |
| 30 | S012 | 电缆 |
+--------+----------+------------+
mysql> select * from adjusment;

结果 :

+----+-------------+-------------+--------+------- -+------------+---------------+
| 编号 | 科德巴朗 | nama_barang | 状态 | 朱姆拉 | 唐加 | 用户 |
+----+-------------+-------------+--------+------- -+------------+---------------+
| 7 | P001 | 主轴 | + | 10 | 2013-03-30 | 管理员 |
| 8 | P001 | 主轴 | - | 5 | 2013-03-30 | 管理员 |
| 9 | S012 | 电缆 | + | 0 | 2013-03-30 | 管理员 |
+----+-------------+-------------+--------+------- -+------------+---------------+

我计算过

select(select sum(jumlah) from adjusment where status='+') - (select sum(jumlah) from adjusment where status='-') as total,kode_barang,nama_barang from adjusment group by kode_barang;
+-------+-------------+-------------+
| 总计| 科德巴朗 | nama_barang |
+-------+-------------+-------------+
| 5 | P001 | 主轴 |
| 5 | 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) + COALESCE((select sum(jumlah) from adjusment where status='+' and kode_barang=a.kode),0) - COALESCE((select sum(jumlah) from adjusment where status='-' and kode_barang=a.kode),0) as total,a.kode,a.nama from warehouse a group by a.kode;
+--------+----------+------------+
| 总计| 科德 | 名 |
+--------+----------+------------+
| 空 | ACLG001 | AC LG 1 包 |
| 165 | P001 | 主轴 |
| 30 | S012 | 电缆 |
+--------+----------+------------+

结果应该是 Cable = 35 和 AC LG 1 PK = 4。

怎么了?

4

1 回答 1

0

格式化后似乎更容易:

SELECT wh.total + COALESCE(adj.total,0) AS total, wh.kode, wh.nama FROM (
    SELECT(
        SELECT SUM(jumlah) FROM warehouse 
        WHERE tgl_invoice >= '2013-03-17' AND tgl_invoice <='2013-03;18' AND kode = a.kode
    ) - COALESCE(
        (SELECT SUM(jumlah) FROM use_material 
        WHERE tanggal >= '2013-03-17' AND tanggal <='2013-03;18' AND kode_barang = a.kode), 
        0
    ) AS total, 
    a.kode, a.nama FROM warehouse AS a 
    GROUP BY a.kode
) AS wh
LEFT JOIN
(
    SELECT(
        SELECT SUM(jumlah) FROM adjusment 
        WHERE status = '+'
    ) - (
        SELECT SUM(jumlah) FROM adjusment 
        WHERE status = '-'
    ) AS total, kode_barang, nama_barang FROM adjusment 
    GROUP BY kode_barang
) AS adj
ON wh.kode = adj.kode_barang
于 2013-03-30T08:01:36.233 回答