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

24CaratAsked:
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.

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

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.