Table join question

Here is some sample data from 2 sql server 2008 tables

CustomerOrderNew
CustomerID   OrderDate      OrderAmount
1                      04/01/2018   400
2                      04/01/2018   666
3                      04/20/2018   787

CustomerOrderOld
CustomerID   OrderDate      OrderAmount
1                      04/01/2018   250
1                      04/11/2018   45
5                     05/01/2018   1000


Trying to write a query that will return rows from both tables.  The “catch” is  if there is an entry in CustomerOrderOld with a matching customerid and orderdate in CustomerOrderNew, I do not want to return it

Given the sample data above I would like the output to be
CustomerID   OrderDate      OrderAmount
1                      04/01/2018   400              
2                      04/01/2018   666
1                      04/11/2018   45
3                      04/20/2018   787
5                     05/01/2018   1000
johnnyg123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
SELECT CustomerID, OrderDate, OrderAmount
FROM CustomerOrderNew
UNION ALL
SELECT CustomerID, OrderDate, OrderAmount
FROM CustomerOrderOld coo
WHERE NOT EXISTS(
    SELECT 1
    FROM CustomerOrderNew con
    WHERE con.CustomerID = coo.CustomerID AND
        con.OrderDate = con.OrderDate
    )
0
johnnyg123Author Commented:
hey scott

Thanks so much for the post!

I get the following
CustomerID      OrderDate      OrderAmount
1      2018-04-01      400
2      2018-04-01      666
3      2018-04-20      787


I reread my question and I apologize for not making it clear that I want the unmatched entries from customerorderold as well
just wanted to skip any matching entries with same customerid and orderdate

Given the sample data above I would like the output to be
 CustomerID   OrderDate      OrderAmount
 1                      04/01/2018   400              
 2                      04/01/2018   666
 1                      04/11/2018   45
 3                      04/20/2018   787
 5                     05/01/2018   1000

Thanks!
0
Scott PletcherSenior DBACommented:
That's what I was trying to do.  Without any usable sample data, I wasn't able to test my code.  Us experts are already volunteering our time to help you, it seems a bit much that we also have to build the test data too(!).  But I'll go ahead and create usable data myself:

CREATE TABLE #CustomerOrderNew (
    CustomerID int NOT NULL,
    OrderDate date NOT NULL,
    OrderAmount int NOT NULL
);
INSERT INTO #CustomerOrderNew VALUES
(1,                    '04/01/2018', 400),
(2 ,                   '04/01/2018', 666),
(3  ,                  '04/20/2018', 787)


CREATE TABLE #CustomerOrderOld (
    CustomerID int NOT NULL,
    OrderDate date NOT NULL,
    OrderAmount int NOT NULL
);
INSERT INTO #CustomerOrderOld VALUES
(1 ,                     '04/01/2018' ,  250),
(1  ,                    '04/11/2018',   45),
(5   ,                  '05/01/2018',   1000)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
Typo in the EXISTS query, wrong alias.  Here's a corrected version:

SELECT CustomerID, OrderDate, OrderAmount
FROM #CustomerOrderNew
UNION ALL
SELECT CustomerID, OrderDate, OrderAmount
FROM #CustomerOrderOld coo
WHERE NOT EXISTS (
    SELECT 1
    FROM #CustomerOrderNew con
    WHERE con.CustomerID = coo.CustomerID AND
        con.OrderDate = coo.OrderDate
    )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverCommented:
Try

select * from customerordernew new
Where not exists(select * from customerorderold old
Where old.customerid=new.customerid
And old.orderdate=new. orderdate)

Open in new window

0
johnnyg123Author Commented:
Thanks Scott!  Appreciate the help

Point taken on the data.  my bad
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.