cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Query to identify customers who only purchased from one warehouse

IldiW
Explorer
0 Kudos
195

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')   
View Entire Topic
SonTran
Active Contributor
0 Kudos

Hi,

Try this

SELECT distinct T0.CardCode
FROM OINV T0 WITH(NOLOCK) INNER JOIN INV1 T1 WITH(NOLOCK) 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 WITH(NOLOCK) INNER JOIN INV1 T1 WITH(NOLOCK) on T0.DocEntry=T1.DocEntry
WHERE T0.DocDate>='2022-01-01' AND T0.DocDate<='2022-02-28' AND T1.WhsCode='01')

Hope this helps,

Son Tran

IldiW
Explorer
0 Kudos

Thank you Son Tran,
I've tried this but the query ran for exactly the same length of time.