• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

JOIN 4 tables and do calculations

Hi everybody,

i'm stuck again with this SQL query.

I used to have this query where i would check the stock.
And compare it with our current pending orders from customers and get from the productlist the product description.

Now i've added a new table partspending to see what pending orders we have on production or sailing (not in stock)

So i can compare stock + sailing against pending orders customers

The code works well but the problem is it only checks the articles that are currently in stock.
If it's not on stock it doesnt show them.

I've been trying to change and adapt the SQL code to something else that works but i just can't seem to get it done.

Here is the code that works but isn't showing the articles that are not on stock.


SELECT l.*, 
g.CombOmschr, 
h.Wanted, 
k.Sailing, 
CASE WHEN ISNULL(k.Sailing) = 1 THEN (h.Wanted-l.Allstock) ELSE (h.Wanted-(l.Allstock+k.Sailing)) END "Short"
 
FROM(SELECT 
    teststock.CombArtNr, 
    SUM(CASE teststock.KlantNr WHEN '0' THEN 0 ELSE 1 END) "Assigned", 
    SUM(CASE teststock.KlantNr WHEN '0' THEN 1 ELSE 0 END) "RestStock", 
    COUNT(teststock.CombArtNr) "AllStock" 
    FROM teststock 
    WHERE CombArtNr Like 'K%'
    GROUP BY teststock.CombArtNr 
)l 
 
 
LEFT JOIN (SELECT 
CONCAT(productlist.artcode,'_',productlist.kleurcode) "CombArtNr", 
CONCAT(productlist.artomschr,' - ',productlist.artomschr2,' - ',productlist.kleuromschr) "CombOmschr" 
FROM productlist 
GROUP BY CombArtNr 
)g 
ON l.CombArtNr = g.CombArtNr 
 
 
LEFT JOIN (SELECT 
CONCAT(prdbesarch.artcode,'_',prdbesarch.klrcode) "CombArtNr", 
SUM(prdbesarch.qty) "Wanted" 
FROM prdbesarch WHERE faktuurnr = '0' AND leveringsnr = '0'
GROUP BY CombArtNr 
)h 
ON l.CombArtNr = h.CombArtNr 
 
 
LEFT JOIN (SELECT 
DISTINCT(LocCombArtCd), 
Aantal-Received "Sailing"
FROM partspending 
WHERE Aantal - Received <> 0  
GROUP BY LocCombArtCd
)k 
ON k.LocCombArtCd = l.CombArtNr 
 
 
ORDER BY l.CombArtNr

Open in new window

0
24Carat
Asked:
24Carat
1 Solution
 
jfmadorCommented:
Since you have 4 left join, the query result will be limit by your first query

Does the item that you are looking for are present in your L query? the field in this query talk about stock, then If they are not present in this query but they are in your G query put the G query first using a left join on the L query.

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now