views:

76

answers:

2

Hey: I am trying to write a query that will return all orders that only have a Subscription included. It is easy enough to write a query that includes all Orders with Subscriptions, another that includes all orders without a Subscription and then compare them with an unmatched query.

But I don't want to have to store Queries in my Access database, I prefer to have it all in my ASP code, and I can't get this to work with just one complex query.

Here are samples of what works if I store them:

Query1

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((Not ((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query2

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query3

SELECT Query2.OrderID, Query2.ProductID
FROM Query2 LEFT JOIN Query1 ON Query2.OrderID = Query1.OrderID
WHERE (((Query1.OrderID) Is Null));

So, my question is 'how do I write Query3 so that it doesn't refer to Query1 or Query2?' or, am I missing some other way do do this?

Thanks, Pete [email protected]

+2  A: 

Assumptions

  • ProductID between 12 and 15 refers to subscriptions.
  • You are looking for all orders with only subscriptions and no other product types.

How about something like this:

SELECT O.OrderID, TOI.ProductID
FROM tblOrders O
      INNER JOIN tblOrderItems TOI ON (O.OrderID = TOI.OrderID)
WHERE (TOI.ProductID between 12 and 15) AND
      NOT EXISTS (SELECT * 
                  FROM tblOrderItems TOI2
                  WHERE (NOT TOI2.ProductID between 12 and 15) AND
                        (TOI2.OrderID=O.OrderID)
                 )
JohnFx
Thanks, John. All of your assumptions are correct -- the problem is that tblOrders doesn't contain ProductID, I need to JOIN to tblOrderItems here.
Pete Augello
oops. got the table wrong in the exists clause. I think I have it fixed now.
JohnFx
John: Sorry, but I'm still confused. What is this: SELECT * FROM tblOrderItems TOI WHERE (NOT TOI.ProductID between 12 and 15) (TOI.OrderID=tblOrders.OrderID)Shouldn't that part be able to stand along? I feel link it's missing a keyword between the )( if you know what I mean. Doesn't there need to be a JOIN there?Thanks again.Pete
Pete Augello
There was, refresh to get my last edit. I left out an "AND"
JohnFx
John -- that's it! I guess I was having trouble with the AS clauses (and I see you left that keyword out -- I didn't know you could do that!)Thanks for all your help.
Pete Augello
An upvote or accepting my answer is all the thanks required. =)
JohnFx
A: 

If you do not want to worry about having a join, here is a way to do it with a pivot table.

     select OrderID, 
     sum(case productID between 12 and 15 then 1 else 0 end) HAS_SUBSCRIPTION,
     sum(case productID between 12 and 15 then 0 else 1 end) HAS_OTHER
     FROM tblOrderItems
     GROUP BY OrderID
     HAVING HAS_SUBSCRIPTION > 0 and HAS_OTHER = 0;
Ryan Goltry