Solved

Simple SQL Query

Posted on 2013-01-21
19
281 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
19 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 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:RQuadling
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:RQuadling
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
 
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:RQuadling
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 73

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:RQuadling
RQuadling earned 250 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:LukeChung-FMS
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 73

Expert Comment

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

Expert Comment

by:RQuadling
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 73

Expert Comment

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

Expert Comment

by:RQuadling
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now