How To Convert Null Value to Zero On LEFT JOIN Query
September 15, 2006 — Arief Fajar NursyamsuI got stuck on my software development process on how to show items that are not completely ordered to sub-contractor. The SQL query result always shows only items that some are already ordered but not yet complete. I need this to warn the production department as well as marketing department.
This article tells how you can easily convert NULL value using MySQL COALESCE function.
I have order table like this:
SELECT p.code AS product_code, pd.qty AS buyer_qty_order, wd.qty AS qty_ordered_to_sub_con FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
WHERE lpp.id = ‘1′
Buyer Order Quantity

Since I have ordered some items to sub contractor, I need to know how many quantity has been ordered.
SELECT p.code AS product_code, pd.qty AS buyer_qty_order,
(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) AS qty_ordered_to_sub_con FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
LEFT JOIN wo_detail wd ON wd.pi_detail_id=pd.id
WHERE lpp.id = ‘1′
GROUP BY pd.id
Sub Contractor Ordered Quantity

NULL value on AU-01A means that this item is not yet ordered to sub contractor.
I also need to know the balance.
SELECT p.code AS product_code, pd.qty AS buyer_qty_order,
(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) AS qty_ordered_to_sub_con,
(pd.qty-(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id)) AS qty_balance FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
LEFT JOIN wo_detail wd ON wd.pi_detail_id=pd.id
WHERE lpp.id = ‘1′
GROUP BY pd.id
Balance

AU-01A has NULL value on both last two columns.
Since I need to eliminate item that is completely ordered to sub contractor, I execute this query and the result is not like what I want. The query only returns items which is ordered but not all.
SELECT p.code AS product_code, pd.qty AS buyer_qty_order,
(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) AS qty_ordered_to_sub_con,
(pd.qty-(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id)) AS qty_balance FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
LEFT JOIN wo_detail wd ON wd.pi_detail_id=pd.id
WHERE lpp.id = ‘1′ AND (SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) < pd.qty
GROUP BY pd.id
Wrong Result

The query returns AU-02 only instead of returning both AU-01A and AU-02.
I have no idea about this at the first time, but after look again at my query result, I notice that the condition:
..AND (SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) < pd.qty ..
is only valid if the result is not returning NULL value, that is why I got the result above.
Reading at MySQL Manual, Null value can be converted to Zero using COALESCE function. Quoting from MySQL Manual:
Therefore I use this function in the query:
SELECT p.code AS product_code, pd.qty AS buyer_qty_order,
(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id) AS qty_ordered_to_sub_con,
(pd.qty-(SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id)) AS qty_balance FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
LEFT JOIN wo_detail wd ON wd.pi_detail_id=pd.id
WHERE lpp.id = ‘1′
AND COALESCE((SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id),0) < pd.qty
GROUP BY pd.id
Result Using COALESCE

Now the result is correct, but still has NULL value on the last two columns. Adding COALESCE at the query gives me the result I want.
SELECT p.code AS product_code, pd.qty AS buyer_qty_order,
COALESCE((SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id),0) AS qty_ordered_to_sub_con,
(pd.qty-COALESCE((SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id),0)) AS qty_balance FROM pi_detail pd
INNER JOIN product p ON p.id = pd.product_id
INNER JOIN lpp ON pd.pi_id = lpp.pi_id
LEFT JOIN wo_detail wd ON wd.pi_detail_id=pd.id
WHERE lpp.id = ‘1′
AND COALESCE((SELECT SUM(wd.qty) FROM wo_detail wd WHERE pd.id=wd.pi_detail_id),0) < pd.qty
GROUP BY pd.id
Correct Result

Finally I can continue my development..
Ups.. here is the screen-shoot for the application.
















September 18, 2006 pukul 12:57 pm
woloh, ngeri ngunu dab kuerimu…..
ajari sql aku….
September 18, 2006 pukul 1:08 pm
Lah sampeyan kan sudah bikin SIKUDA..

Querynya pasti lebih advance daripada punyaku..
He he he he
Silahkan bertanya.. kalau bisa saya jawab..
klo ga bisa… RTFM aja…
Oktober 17, 2006 pukul 11:38 pm
serem..queryne..
Maret 16, 2007 pukul 1:08 pm
Hi,
I use the method COALESCE for my application. But when i run the program, i have this error: Function COALESCE does not exists.
I want to know what might be the problem.Its pretty important for me. I hope to receive a reply soon.
Thanks in adavnce!
Maret 16, 2007 pukul 1:32 pm
Did you use MySQL?
I tested this with MySQL. It is a MySQL function, not in the programming language.
Oktober 16, 2007 pukul 12:13 am
i want to know how to display NULL instead of all zeros in a table while using select query
Januari 18, 2008 pukul 4:58 am
Hey man
me salvaste
GRACIAS
Thanks
April 29, 2008 pukul 5:41 pm
wao….!
it works and my problem is solved dear….
thank u.
Mei 28, 2008 pukul 7:43 pm
Tancks For We
Juni 5, 2008 pukul 12:53 am
Hi,
Your post was quite helpful to me. Could you tell me how the same could be achieved with sqlite. That is, how null could be converted to 0 (or other values) with select statement ?
Rajen.
Juni 5, 2008 pukul 2:58 pm
Oops!! Sorry about that. Sqlite also has a function called coalesce().
Rajen
Juni 6, 2008 pukul 4:14 pm
@ Rajen
Great to know that you solve your problem yourself.
Thanks for visiting
Juli 2, 2008 pukul 12:07 am
this blog showed in google brilliantly when i was looking for a solution for the exact same problem, thanks for the tip!