Selamat Hari Raya Idul Fitri
Mohon Maaf Lahir dan Batin
“Semoga Alloh menerima semua amal ibadah kita. Amin.“
Selamat Hari Raya Idul Fitri
Mohon Maaf Lahir dan Batin
“Semoga Alloh menerima semua amal ibadah kita. Amin.“
I 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.
I digged forumponsel.com just now to find my post regarding how to download ringtones and images from WAP Site with Nokia 3105 (a CDMA Phone). I didn’t have a data cable that time, so I tried to upload the images and ringtones on my website and then accessed them from phone’s wap browser directly. But the result is the ringtones are played and the images are showed rather than dowloaded to the phone.
I asked the Nokia Priority Care and they said much better if I went to the city mall to buy ringtones and images from upload service booth 😦
I am not easily gave up, so I went to Google and worked with Nokia Mobile Internet Toolkit and Nokia Content Publishing Toolkit. I found some solutions for this download problem.
Easy Ways:
Hard Way:
To do this, you need to have a webspace on the Internet and you are able to add .htaccess file. Then follow this step by step instruction:
<media xmlns=”http://www.openmobilealliance.org/xmlns/dd”> <objectURI>http://your-web-address.com/image_name.gif</objectURI> <size>Your File Size In Byte (ie: 2400)</size>
<type>Your Data Type (ie:image/gif) </type> <type>application/vnd.oma.drm.message</type> <name>file_name_for_phone.gif</name>
<vendor>Vendor Name (ie: Up To You Vendor)</vendor>
<description>File Description</description> <nextURL>http://url-after-successful-download</nextURL>
</media>
The instruction above is a hard way for you to add a content to your Nokia 3105, so I think a data cable can make your life easier. But, in the other hand, the sample above can make you think that you can start your own content provider business.
Note: With a little PHP programming, you can create a dynamic WAP download site and dynamically create DD files on the fly. I made a PHP version for this, I will post it here soon.
This post has been moved. Click here.