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.*, 
CASE WHEN ISNULL(k.Sailing) = 1 THEN (h.Wanted-l.Allstock) ELSE (h.Wanted-(l.Allstock+k.Sailing)) END "Short"
    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 
CONCAT(productlist.artcode,'_',productlist.kleurcode) "CombArtNr", 
CONCAT(productlist.artomschr,' - ',productlist.artomschr2,' - ',productlist.kleuromschr) "CombOmschr" 
FROM productlist 
ON l.CombArtNr = g.CombArtNr 
CONCAT(prdbesarch.artcode,'_',prdbesarch.klrcode) "CombArtNr", 
SUM(prdbesarch.qty) "Wanted" 
FROM prdbesarch WHERE faktuurnr = '0' AND leveringsnr = '0'
ON l.CombArtNr = h.CombArtNr 
Aantal-Received "Sailing"
FROM partspending 
WHERE Aantal - Received <> 0  
ON k.LocCombArtCd = l.CombArtNr 
ORDER BY l.CombArtNr

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.