Solved

Help with selection query

Posted on 2014-09-17
4
70 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 45

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now