Solved

Help with selection query

Posted on 2014-09-17
4
76 Views
Last Modified: 2014-11-26
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
0
Comment
Question by:GiaHughes
4 Comments
 
LVL 2

Expert Comment

by:Akilandeshwari N
ID: 40327686
You are not joining all the tables mentioned in the FROM clause. You have joined WOEQLIST and WO tables but not WOC.
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40327865
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.
0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40327928
Use NOT EXISTS:
Select WOEQLIST.WONUM, WOEQLIST.EQNUM, WOEQLIST.CLOSEDATE 
 from WOEQLIST, WO
 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 not exists (select 1 from WOC where WOEQLIST.WONUM = WOC.WONUM)

Open in new window

0
 

Author Closing Comment

by:GiaHughes
ID: 40466668
Works great
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

840 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