I have some data from two tables:
Table prod_schedule
Line Model Lot_no Quantity Lot_Quantity
FA 01 KD-R330JD 060A 240 1000
FA 01 KD-R330JD 060A 260 1000
FA 02 KD-R330JD 060A 400 1000
FA 02 KD-R330JD 060A 100 1000
Table inspection_report
Line Model Lot_no Merchandise
FA 01 KD-R330JD 060A 200
FA 01 KD-R330JD 060A 300
FA 02 KD-R330JD 060A 500
I want to show data like:
Line Model Lot_no Merchandise Quantity
FA 01 KD-R330JD 060A 500 500
FA 02 KD-R330JD 060A 500 500
And I try some query like:
SELECT A.Line, TRIM(A.Model) AS Model,A.Lot_no,B.Quantity,A.Merchandise
FROM inspection_report A
LEFT JOIN prod_schedule B
ON A.Line= B.Line_Name AND CONCAT('%',B.Model_Code,'%') LIKE CONCAT('%',A.Model,'%')
AND A.Lot_no=B.Lot_No_
WHERE A.Model LIKE '%330%' AND A.Lot_no LIKE '%060%'
GROUP BY A.Line,A.Model,A.Lot_no,Merchandise
But I get result like :
Line Model Lot_no Merchandise Quantity
FA 01 KD-R330JD 060A 200 240
FA 02 KD-R330JD 060A 300 240
[ Post Overhaul ]
With the most recent update of the question, here’s the code that would generate the desired output:
And this is..
I aggregated data from each table and then joined them after the grouping. Version 2 places the
WHEREclause in both sub queries; please test which is faster for you.