I have a query that joins across multiple tables with orderline being the main table.
Select sp.sale_code,
oh.ware_code,
cust.cust_code,
oh.activation_date,
oh.last_del_date,
oh.order_date,
ol.order_num,
ol.line_num,
ol.entry_date,
oh.ship_date,
trim(wrd.warereptgrp_code) warereptgrp_code,
pr.maingrp_code,
pr.prodgrp_code,
pr.class_code,
pr.cat_code,
ol.part_code,
pr.desc_text,
oh.quote_amt / 1.1 quote_amt,
ol.line_tot_amt / 1.1 line_tot_amt,
ol.order_qty,
(ol.line_tot_amt / ol.order_qty) / 1.1 product_price,
dd.picked_qty,
dh.pick_date,
ol.uom_code,
pr.weight_qty,
pr.cubic_qty,
pr.area_qty,
pr.length_qty,
oh.ord_ind,
ol.back_qty
from orderline ol
JOIN ordhead AS oh ON oh.order_num = ol.order_num AND oh.cmpy_code = ol.cmpy_code
LEFT OUTER JOIN product AS pr ON pr.part_code = ol.part_code AND pr.cmpy_code = ol.cmpy_code
LEFT OUTER JOIN category AS c ON c.cat_code = pr.cat_code AND c.cmpy_code = ol.cmpy_code
JOIN warereptdetl AS wrd ON wrd.ware_code = oh.ware_code AND wrd.cmpy_code = ol.cmpy_code
JOIN salesperson AS sp ON oh.sale_code = sp.sale_code AND oh.cmpy_code = sp.cmpy_code
JOIN customer AS cust ON oh.cust_code = cust.cust_code AND oh.cmpy_code = cust.cmpy_code
JOIN delivhead AS dh ON dh.order_num = oh.order_num AND dh.cmpy_code = oh.cmpy_code
JOIN delivdetl AS dd ON oh.order_num = dd.order_num AND oh.cmpy_code = dd.cmpy_code AND ol.line_num = dd.order_line_num AND ol.part_code = dd.part_code AND dh.pick_num = dd.pick_num
where ol.cmpy_code = 'AB'
and dh.pick_date BETWEEN '31/12/2019' AND '31/01/2020'
and OH.ord_ind = 7
and oh.quote_flag <> 'Y'
and dh.status_ind = 1
and pr.class_code in ('BRTILE','TAGT','TILE','TLAB','LABQLD','TRES')
and wrd.warereptgrp_code in ('BRNSW','BRVIC','BRQLD','BRSA','BRTAS','BRWA')
The orderline table has multiple rows of data per 1 order_no.
Within the order are products which "usually" have a part_code. There are occasions where a line within an order will NOT have a part_code this typically happens when an order has an extra charge for delivery or a training levy which doesn't have an actual product associated to it.
Currently my query will ONLY return the data where a part_code exists so it omits all the extra charges which have no part_code.
As an example, if I do Select * from orderline where order_num = '6033349'
I get 6 lines returned and the 6th line has nothing in the part_code column.
If I lock my query above to only pull results for that order_num it only returns the first 5 rows omitting the 6th line which has no part_code. I need to modify this query to ensure that ALL results from the orderline are returned even if they don't have a part_code.
The linked answer didn't help me as I tried moving the where clause into the join like so LEFT OUTER JOIN product AS pr ON (pr.part_code = ol.part_code and pr.class_code in ('BRTILE','TAGT','TILE','TLAB','LABQLD','TRES')) or ol.part_code is null
however it still did not return the values on the orderline table which have a null part_code.
Update: I have taken @Jonathan Leffler advice and cut the query right down to bare minimum and with his suggestion this now brings back the extra row with no part_code form the ol table:
Select ol.entry_date,
ol.part_code,
ol.order_qty,
pr.weight_qty,
pr.cubic_qty,
pr.area_qty,
pr.length_qty,
ol.back_qty
from orderline ol
LEFT JOIN product AS pr ON pr.part_code = ol.part_code
LEFT JOIN category AS c ON c.cat_code = pr.cat_code
where (pr.class_code IS NULL OR pr.class_code IN ('BRTILE', 'TAGT', 'TILE', 'TLAB', 'LABQLD', 'TRES'))
and ol.order_num = '6033349'
Now I need to work out the difference between the first query and the second to return this data in the first one.
RESOLVED:
The issue was that the delivdetl table also needed to be a left outer join LEFT OUTER JOIN delivdetl AS dd ON oh.order_num = dd.order_num AND ol.line_num = dd.order_line_num AND ol.part_code = dd.part_code AND dh.pick_num = dd.pick_num
To determine this I commented out all the joins and selects and brought them back one by one until I could reproduce the error. Bringing back the delivdetl join made the extra line from ol dissapear. Updating the join resolved it and brought the elusive 6th line in.