?
Solved

Simple SQL Query

Posted on 2013-01-21
19
Medium Priority
?
334 Views
Last Modified: 2013-01-29
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
0
Comment
Question by:JArndt42
  • 6
  • 6
  • 4
  • +1
17 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 38801997
select * from custmas
where customer_code in (select customer_code from order where load_year = 2011)
and customer_code not in (select customer_code from order where load_year = 2012)
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 38802025
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.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 38802049
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'
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 1

Author Comment

by:JArndt42
ID: 38802090
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

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 38802121
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!!!)
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38802123
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
0
 
LVL 1

Author Comment

by:JArndt42
ID: 38802151
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.
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 1000 total points
ID: 38802350
If you can get the 2 sub queries to return the correct values, then the IN(2011 query) AND NOT IN(2012 query) should be OK.
0
 
LVL 10

Expert Comment

by:Luke Chung
ID: 38805775
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.
0
 
LVL 1

Author Comment

by:JArndt42
ID: 38829284
It worked. Thank you.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38829575
Isn't the accepted answer just a rephrasing of the original post?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 38830116
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?
0
 
LVL 1

Author Comment

by:JArndt42
ID: 38831406
Actually I have no idea on how to do that. instructions please.
0
 
LVL 1

Author Comment

by:JArndt42
ID: 38831473
Yes please do split the points. You were both helpful. Thank you very much.
0
 
LVL 1

Author Closing Comment

by:JArndt42
ID: 38831553
thank you both very much for your help.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38831565
glad we could help, and thank you !
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 38832557
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question