Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple SUM and multiple calculated values
    primarykey
    data
    text
    <p>After calculating incoming material and use of material in my system, there is an adjustment item if someone wants to do the adjustment.</p> <p>QUERY Incoming Material - Use of 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) - COALESCE((select sum(jumlah) from use_material where tanggal &gt;= '2013-03-17' AND tanggal &lt;='2013-03;18' and kode_barang=a.kode),0) total, a.kode, a.nama from warehouse a group by a.kode; </code></pre> <pre> +-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | 4 | ACLG001 | AC LG 1 pk | | 160 | P001 | Spindle | | 30 | S012 | Cable | +-------+---------+------------+ </pre> <pre><code>mysql&gt; select * from adjusment; </code></pre> <p>result :</p> <pre> +----+-------------+-------------+--------+--------+------------+---------------+ | 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 | +----+-------------+-------------+--------+--------+------------+---------------+ </pre> <p>I have calculated</p> <pre><code>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; </code></pre> <pre> +-------+-------------+-------------+ | total | kode_barang | nama_barang | +-------+-------------+-------------+ | 5 | P001 | Spindle | | 5 | S012 | Cable | +-------+-------------+-------------+ </pre> <p>And my query for last stock like this:</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) + 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; </code></pre> <pre> +-------+---------+------------+ | total | kode | nama | +-------+---------+------------+ | NULL | ACLG001 | AC LG 1 pk | | 165 | P001 | Spindle | | 30 | S012 | Cable | +-------+---------+------------+ </pre> <p>The result should be Cable = 35 and AC LG 1 PK = 4.</p> <p>What's going wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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