- Code: Select all
SELECT itemreceiptdetail.itemid,
item.itemname,
SUM(itemreceiptdetail .quantity) AS total_receipt,
itemreceiptdetail.unit,
Coalesce(SUM(itemconsumptiondetail.quantity), 0) AS total_consumed,
Ifnull(itemconsumptiondetail.unit, ''),
SUM(itemreceiptdetail .quantity) -
Coalesce(SUM(itemconsumptiondetail.quantity), 0) AS stock
FROM item,
itemconsumptiondetail
RIGHT JOIN itemreceiptdetail
ON ( itemreceiptdetail.itemid = itemconsumptiondetail.itemid )
WHERE itemreceiptdetail.itemid = item.itemid
GROUP BY itemreceiptdetail.itemid;
HASIL

- Code: Select all
+--------------+----------+---------------+------+----------------+---------------------------------------+-------+
| itemID | itemName | Total_Receipt | unit | Total_Consumed | IFNULL(ItemConsumptionDetail.unit,'') | Stock |
+--------------+----------+---------------+------+----------------+---------------------------------------+-------+
| 01010-50616 | BOLT | 16 | Pcs | 4 | Pcs | 12 |
| 01010-50816 | BOLT | 5 | Pcs | 4 | Pcs | 1 |
| 01010-50820 | BOLT | 10 | Pcs | 10 | Pcs | 0 |
| 01010-50835 | BOLT | 5 | Pcs | 0 | | 5 |
| 01010-51016 | BOLT | 10 | Pcs | 0 | | 10 |
| 01601-20410 | WASHER | 5 | Pcs | 1 | Pcs | 4 |
| 09244-02508 | PIN | 30 | Pcs | 5 | Pcs | 25 |
| 150-70-21346 | BIT,R.H. | 4 | Pcs | 2 | Pcs | 2 |
| 150-70-21356 | BIT,L.H. | 4 | Pcs | 0 | | 4 |
| 175-78-31230 | POINT | 40 | Pcs | 0 | | 40 |
+--------------+----------+---------------+------+----------------+---------------------------------------+-------+
Tampilan keluaran sesuai yang gw harapkan, tapi seperti yang gw tandai merah, nilainya 16 (itemID=01010-50616), padahal aslinya cuma 8 pada gambar dibawah.

- Code: Select all
SELECT * FROM ItemReceiptDetail WHERE itemID='01010-50616';
+---------------------+---------------+-------------+----------+------+--------+
| itemReceiptDetailID | itemReceiptID | itemID | quantity | unit | remark |
+---------------------+---------------+-------------+----------+------+--------+
| 25 | 1002 | 01010-50616 | 8 | Pcs | |
+---------------------+---------------+-------------+----------+------+--------+
Apa yang salah ya om pada perintah select gw ya? kalau ada ide lain boleh lah di share
. Thanks


Karena saya tidak tahu persis struktur table-tablenya, saya cuma bisa kasi saran untuk mencoba-coba pemakaian Right Join atau Left Join sebagai klausa penghubung table-tablenya.