Skip to Content
0
May 25 at 12:53 AM

Query to identify customers who only purchased from one warehouse

34 Views

Hi Team,

I'm trying to identify customers who purchased from one warehouse but not from the other and I have this query, which is working and giving the correct answer but takes nearly 3 minutes to run, even though the timeframe is very short (only two months). Is there a way to simplify this to make the query faster?
Or maybe I'm approaching it completely wrong with the subquery?

Many thanks,
Ildi

SELECT distinct T0.CardCode
FROM OINV T0 INNER JOIN INV1 T1 on T0.DocEntry=T1.DocEntry 
WHERE T0.DocDate>='2022-01-01' AND T0.DocDate<='2022-02-28' AND T1.WhsCode='08' 
AND T0.CardCode NOT IN (SELECT distinct T0.CardCode
FROM OINV T0 INNER JOIN INV1 T1 on T0.DocEntry=T1.DocEntry
WHERE T0.DocDate>='2022-01-01' AND T0.DocDate<='2022-02-28' AND T1.WhsCode='01')