sql SUM: change null with 0
<p>I have a query like this: </p>

<pre><code>SELECT bgdepartemen.c_kodedept AS c_kodedept,
       bgdepartemen.vc_namadept AS vc_namadept,
       bgdivisi.c_kodediv AS c_kodediv,
       bgdivisi.vc_namadiv AS vc_namadiv,
       bggroup0.c_kodegrp0 AS c_kodegrp0,
       bggroup0.vc_namagrp0 AS vc_namagrp0,
       (SELECT COALESCE(SUM(bgbudget0.n_nilai))
        FROM (bgbudget0 JOIN bggroup1 ON bgbudget0.c_kodegrp1 = bggroup1.c_kodegrp1)
        WHERE bgbudget0.n_tahun = 2016 AND
              bgbudget0.n_bulan &gt;= 2 AND
              bgbudget0.n_bulan &lt;= 3 AND
              bgbudget0.c_kodediv = bgdivisi.c_kodediv AND
              bggroup1.c_kodegrp0 = bggroup0.c_kodegrp0 AND
              bggroup1.c_kodegrp1 LIKE '%') AS nilai
FROM bgdivisi JOIN bggroup0 ON 1 = 1
JOIN bgdepartemen on bgdivisi.c_kodedept = bgdepartemen.c_kodedept
WHERE bgdivisi.c_kodediv LIKE '%' AND
     bgdepartemen.c_kodedept LIKE '%' AND
     bggroup0.c_kodegrp0 LIKE '%'
</code></pre>

<p>And another like this:</p>

<pre><code>SELECT bgdepartemen.c_kodedept AS c_kodedept,
       bgdepartemen.vc_namadept AS vc_namadept,
       bgdivisi.c_kodediv AS c_kodediv,
       bgdivisi.vc_namadiv AS vc_namadiv,
       bggroup0.c_kodegrp0 AS c_kodegrp0,
       bggroup0.vc_namagrp0 AS vc_namagrp0,
       (SELECT COALESCE(SUM(bgrealisasi0.n_nilai))
        FROM (bgrealisasi0 JOIN bggroup1 ON bgrealisasi0.c_kodegrp1 = bggroup1.c_kodegrp1)
        WHERE bgrealisasi0.n_tahun = 2016 AND
        bgrealisasi0.n_bulan &gt;= 2 AND
        bgrealisasi0.n_bulan &lt;= 3 AND
        bgrealisasi0.c_kodediv = bgdivisi.c_kodediv AND
        bggroup1.c_kodegrp0=bggroup0.c_kodegrp0 AND
        bggroup1.c_kodegrp1 LIKE '%') AS nilai
FROM bgdivisi JOIN bggroup0 ON 1 = 1
JOIN bgdepartemen on bgdivisi.c_kodedept = bgdepartemen.c_kodedept
WHERE bgdivisi.c_kodediv LIKE '%' AND
      bgdepartemen.c_kodedept LIKE '%' AND
      bggroup0.c_kodegrp0 LIKE '%'
</code></pre>

<p>I want to select from those tables, with condition</p>

<pre><code> WHERE a.c_kodedept = b.c_kodedept AND
            a.c_kodediv = b.c_kodediv AND
            a.c_kodegrp0 = b.c_kodegrp0 AND
            (a.nilai is not null or b.nilai is not null)
</code></pre>

<p>But I also want to change the null record which appears with 0, I've tried <code>COALESCE</code> but it still doesn't give me the right result.</p>