Solved

Help with selection query

Posted on 2014-09-17
4
79 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 50

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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