Link to home
Start Free TrialLog in
Avatar of Gordon Hughes
Gordon HughesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with selection query

I am trying to find the records that exist in a table (WOEQLIST0 that do not exist in another table (WOC)
with a sub selection within the table (WOEQLIST)
I have tried
Select WOEQLIST.WONUM, WOEQLIST.EQNUM, WOEQLIST.CLOSEDATE from WOEQLIST, WO, WOC
where WOEQLIST.WONUM = WO.WONUM and WO.CLOSEDATE = '1900-01-01 00:00:00.000' and
WO.REQUESTDATE < '2014-01-01 00:00:00.000' and WOEQLIST.WONUM <> WOC.WONUM

But it does not work, stopped it after bring back 36K

Gordon
Avatar of Akilandeshwari N
Akilandeshwari N

You are not joining all the tables mentioned in the FROM clause. You have joined WOEQLIST and WO tables but not WOC.
Try this:

Select W1.WONUM,
   W1.EQNUM, 
   W1.CLOSEDATE from WOEQLIST W1
   LEFT JOIN WOC W2 ON W2.WONUM = W1.WONUM
 where W2.CLOSEDATE = '1900-01-01 00:00:00.000' 
 and 
 W2.REQUESTDATE < '2014-01-01 00:00:00.000' 
 and W2 IS NULL

Open in new window



OR

SELECT Select W1.WONUM,
   W1.EQNUM, 
   W1.CLOSEDATE 
FROM  WOEQLIST W1
WHERE W2.CLOSEDATE = '1900-01-01 00:00:00.000' 
 and 
 W2.REQUESTDATE < '2014-01-01 00:00:00.000' 
AND NOT EXISTS 
    (SELECT * 
     FROM WOC W2 
     WHERE W1.WONUM = W2.WONUM)

Open in new window


I think W2.CLOSEDATE should be W1.CLOSEDATE but I let you decide that.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

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
Avatar of Gordon Hughes

ASKER

Works great