?
Solved

Help with selection query

Posted on 2014-09-17
4
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

764 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