Link to home
Start Free TrialLog in
Avatar of JArndt42
JArndt42Flag for United States of America

asked on

Simple SQL Query

I have a very simple request but for some reason I can not get my mind around how to do it. I have two tables. ORDER and CUSTMAST. I need to see the customers that did loads in 2011 but not in 2012. The customer in ORDER is ORCUST and the customer code in CUSTMAST is CUCODE.

See simple enough. Help please
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all, calling a table 'ORDER' is a monumentally bad idea. It is a keyword in SQL and is simply going to cause you a problem.

But, changing it to ORDERS or ORDMAST (keeping the convention you have with CUSTMAST - convention is useful!), then adapt this
SELECT custmast.*, ordmast.*
FROM custmast INNER JOIN ordmast ON custmast.cucode = ordmast.orcust
WHERE year(ordmast.date) = 2011

Open in new window


See http://webcheatsheet.com/SQL/access_functions/year.php for more info on the YEAR() function.
Oh. Sorry. Having just seen sdstuber's answer. Yeah. Much better.

You MAY get some performance increase if you limit the number of rows in the sub queries to just 1 row. You don't need ALL the 2011 or 2012 ordes, just 1.

select * from custmas
where customer_code in (select top 1 customer_code from order where load_year = 2011)
and customer_code not in (select top 1 customer_code from order where load_year = 2012)

Open in new window


But with the same potential issue of the table name being 'ORDER'
Avatar of JArndt42

ASKER

RQ,
I am not able to change the table names. this is a pass through query to an AS400 back end. I understand the naming conventions but the database developer that did the program on the back end did not or didn't care. I do many queries with that table and have yet had any issues. This is what it looks like per your request and it returns customers with orders in 2012 as well as customers in 2011. ?????
SELECT CUCODE,CUNAME,DATE(SUBSTR(DIGITS(000000+CULACT),2,7)) AS LASTLOAD
FROM CUSTMAST
WHERE CUCODE IN (SELECT ORCUST FROM ORDER WHERE ORPDAT BETWEEN 2011001 AND 2011365 AND ORSTAT <>'C')
AND CUCODE NOT IN(SELECT ORCUST FROM ORDER WHERE ORPDAT BETWEEN 2012001 AND 2012365 AND ORSTAT <>'C')

Open in new window

If you run the 2 sub queries independently, do they produce the correct results?

Add the ORPDAT column the the output of the subqueries temporarily to make sure that they are being accessed correctly.

And a SELECT DISTINCT may be more efficient (not TOP 1 - what was I thinking!!!)
Avatar of Sean Stuber
Sean Stuber

you have an additional   criteria in your subqueries.

if you have ordcust 1234,  2011001  with ordstat 'A'
and ordcust 1234, 2012001 with ordstat 'C'


it'll show up in your result set
Very slow query. I used distinct in the subs. I am thinking about running the two queries that return a single set of records. One for 2011 and another query for 2012. Then do an ACCESS query to get the results. I wanted it in just one pass through though. the query in plain English is as follows

Show me the customer code, customer name and the date of the last load
from the customer master
where the customer had orders in 2011 and the order was not canceled
and the customer did not have orders in 2012 and the order was not canceled.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Take a look at my paper on creating outer join queries: http://www.fmsinc.com/MicrosoftAccess/query/outer-join/index.html

You'll want to create a query that has all the people who purchased in 2012. Then you want to  link to it from your main table where you filter for 2011 customers who are NOT IN the 2012 list.
It worked. Thank you.
Isn't the accepted answer just a rephrasing of the original post?
I think a split with sdstuber would be a better option here.

And just as an aside, as 2012 was a leap year, should the end date be 2012366?
Actually I have no idea on how to do that. instructions please.
Yes please do split the points. You were both helpful. Thank you very much.
thank you both very much for your help.
glad we could help, and thank you !
Yep! Glad to have helped.

And JArndt42, in the future, if more than one expert has helped provide you with enough information to get your answer, then you can choose to award points to multiple solutions.