sql server - T-SQL: Trying to combine two queries into one -


i want list of customers , purchases among items a, b, c, d, e

but ...

only customers have purchased c or e.

right i'm thinking of pseudo-code:

select customer, item purchases items in (a, b, c, d, e) , customer in  ( select customer purchases items in (c, e) ) 

my actual query more complex, takes long run , includes customer information multiple tables , purchases spread on multiple tables ... wondering if inefficient run select statement twice - , whether can more efficiently?

the answer depends on complexity, format , amount of data can replace in exists other way using temp tables - populate list of customers use items c,e temp tables , use temp table in query

select customer, item purchases p1 p1.items in (a, b, c, d, e)   , exists (     select top 1 1 purchases p2        p1.customer = p2.customer         , p2.items in (c,e)) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -