Problem: My Query returns NULL Potential issues: Subquery formatted wrong and only works if some data I want to sort out is in the dataset.
Example of today's code:
SELECT production_order
,SUM(total_working_time_h) - (SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')) AS total_working_time_h_edit
,SUM(no_of_defects) AS no_of_defects_during_production
FROM {$table}
WHERE production_order = '$production_order'
This works great as long as I have either "Rework" and/or "'出货检验 | OQC'" logged in my database for this production order. If not, I won't get any data at all but NULL. So my problem is somewhere in my subquery I think:
SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')
I've tried adding a "0" to make sure it's get 0 and not NULL without success like follows:
(0 + SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')
Example dataset formated as text:
production_order part_nr station total_working_time_h
26135 129-108816B-UL 压接 | Crimping 1.42
26135 129-108816B 线束组装 | Harness Assembling 7.67
26135 129-108816 测试 | Testing 0.83
26135 129-108816B 外观全检 | Appearance inspection 0.83
Gives this output:
production_order,total_working_time_h_edit
26135, NULL
I want this output:
production_order,total_working_time_h_edit
26135, 10,45
If my dataset hold the station I want to sort out or the production_type I want to sort out everything works as it should. But if both of those is missing in the dataset it returns NULL, since what I think is because the subquery returns 0.
An example of different datasets I'm using. If I have the data marked with green cells in the dataset it will work. If not, I get the return NULL as above.
So how to make my subquery to not return NULL?