Note that there are some explanatory texts on larger screens.

plurals
  1. POASK MySQL Query SUM different table
    text
    copied!<p>Hello i want to ask about sum query, I experienced confusion when performing a query.</p> <p>I want to calculate the total current.</p> <p>SUM For INCOMING Material</p> <pre><code>select (select sum(jumlah) from warehouse where tgl_invoice &gt;= '2013-03-17' AND tgl_invoice &lt;='2013-03;18' and kode=a.kode) total,a.kode,a.nama from warehouse a group by a.kode; </code></pre> <p>Result :</p> <hr> <pre> +-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | 4 | ACLG001 | AC LG 1 pk | | 180 | P001 | Spindle | | 40 | S012 | Cable | +-------+---------+------------+ </pre> <p>SUM For <strong>use of material (goods)</strong></p> <pre><code>select (select sum(jumlah) from use_material where tanggal &gt;= '2013-03-17' AND tanggal &lt;='2013-03;18' and kode_barang=a.kode) total,a.kode,a.nama from warehouse a group by a.kode; </code></pre> <p>Result : </p> <hr> <pre> +-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | NULL | ACLG001 | AC LG 1 pk | | 20 | P001 | Spindle | | 10 | S012 | Cable | +-------+---------+------------+ </pre> <p>And now I want to SUM <strong>Incoming Material(goods)</strong> minus <strong>use of material (goods)</strong></p> <pre><code> select (select sum(jumlah) from warehouse where tgl_invoice &gt;= '2013-03-17' AND tgl_invoice &lt;='2013-03;18' and kode=a.kode) - (select sum(jumlah) from use_material where tanggal &gt;= '2013-03-17' AND tanggal &lt;='2013-03;18' and kode_barang=a.kode) total,a.kode,a.nama from warehouse a group by a.kode; </code></pre> <p>Result :</p> <hr> <pre> +-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | NULL | ACLG001 | AC LG 1 pk | | 160 | P001 | Spindle | | 30 | S012 | Cable | +-------+---------+------------+ </pre> <p>In here is an error in calculating. should AC LG 1 PK must be valued 4. but in result NULL</p> <p>Please Answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload