Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show orders where all products are available. Possible?

Status
Not open for further replies.

BiggerBrother

Technical User
Sep 9, 2003
702
GB
I'm not sure that this is even possible with only a single query, but if it is, hopefully someone here can set me straight.

I have the standard order, and order_products tables, linked by order_id fields. Order table holds the customer id, order date etc, and the order_products table holds the product_id, quantity and order_id.

I have been asked to find a way to produce a report on all orders where they are completable. For example, given the fact that product A,B,C and D are in stock, only return those orders which have ordered one or more of those products. If the order also includes product E, currently not in stock, then don't return that order number.

Is this possible?

Many thanks for any ideas.

BB
 
Code:
select order_id
  from order_products
inner
  join products
    on products.product_id
     = order_products.product_id  
group
    by order_id
having count(*)
     = sum(case when products.instock > 0
                then 1 else 0 end)

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top