2013-03-30 84 views
0

在計算我的系統中傳入的材料和材料的使用之後,如果有人想要進行調整,則會有一個調整項目。多個SUM和多個計算值

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; 
 
+-------+---------+------------+ 
| total | kode | nama  | 
+-------+---------+------------+ 
|  4 | ACLG001 | AC LG 1 pk | 
| 160 | P001 | Spindle | 
| 30 | S012 | Cable  | 
+-------+---------+------------+ 
mysql> select * from adjusment; 

結果:

 
+----+-------------+-------------+--------+--------+------------+---------------+ 
| id | kode_barang | nama_barang | status | jumlah | tanggal | user   | 
+----+-------------+-------------+--------+--------+------------+---------------+ 
| 7 | P001  | Spindle  | +  |  10 | 2013-03-30 | Administrator | 
| 8 | P001  | Spindle  | -  |  5 | 2013-03-30 | Administrator | 
| 9 | S012  | Cable  | +  |  0 | 2013-03-30 | Administrator | 
+----+-------------+-------------+--------+--------+------------+---------------+ 

我算

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; 
 
+-------+-------------+-------------+ 
| total | kode_barang | nama_barang | 
+-------+-------------+-------------+ 
|  5 | P001  | Spindle  | 
|  5 | S012  | Cable  | 
+-------+-------------+-------------+ 

而且我去年股票這樣的查詢:

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; 
 
+-------+---------+------------+ 
| total | kode | nama  | 
+-------+---------+------------+ 
| NULL | ACLG001 | AC LG 1 pk | 
| 165 | P001 | Spindle | 
| 30 | S012 | Cable  | 
+-------+---------+------------+ 

結果應該是電纜= 35 和AC LG 1 PK = 4

什麼錯?

+0

我想,2013-03; 18只是一個錯字?也許你必須使用BETWEEN來比較日期?你能提供一個數據庫計劃嗎? (用於涉及表) – bestprogrammerintheworld

+0

我有編輯日期:) 數據庫模式到哪個表? – yusronnube

+0

你的問題是什麼?這與http://stackoverflow.com/questions/15715323/ask-mysql-query-sum-different-table有何不同? – Barmar

回答

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 
+1

謝謝兄弟,這是工作。 第5行不是SUM,而是FROM像這樣(SELECT SUM(jumlah)FROM use_material – yusronnube

+0

感謝您的更正,一定是點擊+粘貼得太快。 –