SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 comparing 2 views
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rbh123456789
Starting Member

15 Posts

Posted - 05/16/2012 :  10:42:40  Show Profile  Reply with Quote
Hey guys,

I have two views.

View1: A list of Orders that must be resolved. Fields are CompanyID (not unique) and OrderDate (unique only when the CompanyID is involved).

View2: A list of Completed Orders. Fields are CompanyID and Original_OrderDate

There is no 'orderID' to link between the two views. However, there is only ever 1 order per day per Company.

Basically, I am trying to find out which Orders are Outstanding (eg. View1 is missing its match in View2).

So i need to be able to tell SQL to look at View1, then match up the CompanyIDs, and only show the View2 entries that do NOT have a matching OrderDate.

Any help would be appreciated.


robvolk
Most Valuable Yak

USA
15568 Posts

Posted - 05/16/2012 :  11:00:43  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT CompanyID, OrderDate FROM View1
EXCEPT
SELECT CompanyID, OrderDate FROM View2
Go to Top of Page

rbh123456789
Starting Member

15 Posts

Posted - 05/16/2012 :  12:06:17  Show Profile  Reply with Quote
Thanks robvolk.
one more question. i have another field in view1, which doesn't exist in view2, that i want to display in the results.
how do i achieve that?
Go to Top of Page

robvolk
Most Valuable Yak

USA
15568 Posts

Posted - 05/16/2012 :  12:08:00  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT CompanyID, OrderDate, column3 FROM View1 A
WHERE NOT EXISTS(SELECT * FROM View2 WHERE CompanyID=A.CompanyID AND OrderDate=A.OrderDate)
Go to Top of Page

rbh123456789
Starting Member

15 Posts

Posted - 05/16/2012 :  12:26:28  Show Profile  Reply with Quote
robvolk, THANK YOU! this query actually runs a lot faster too. thanks again.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.25 seconds. Powered By: Snitz Forums 2000