Solved

Select All form one if not in the other

Posted on 2010-08-17
5
335 Views
Last Modified: 2012-05-10
I would like to Select all the fields from a table only if the contents of the field "LogID" does not exist in another table.

Can Someone help please.

Below is what I have come up with but it does not like the <> in the Query

SELECT * FROM CallBack

INNER Join Callback_Sent ON CallBack_Sent.LogID <> Callback.LogID

WHERE CallBack.ProbClass = '209' AND CallBack.OpenClose = 'O'

Open in new window

0
Comment
Question by:xeroxcanada
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 125 total points
ID: 33456349
SELECT * FROM CallBack
left Join Callback_Sent ON CallBack_Sent.LogID = Callback.LogID
WHERE CallBack.ProbClass = '209' AND CallBack.OpenClose = 'O'
and Callback.LogID is null
or
SELECT * FROM CallBack
WHERE CallBack.ProbClass = '209' AND CallBack.OpenClose = 'O'
and not exists (select 1 from Callback_Sent whereCallBack_Sent.LogID = Callback.LogID)
0
 
LVL 5

Expert Comment

by:Vaulden
ID: 33456630
I am by no means a SQL expert, but I'll give this a shot. I don't believe you can use the <> in the place you are attempting to. You are telling it which fields to link, it's not yet looking at the data. I think you want to do something like the attached code. This is assuming you want entries from CallBack that do not exist in CallBack_Sent.

SELECT * FROM CallBack

LEFT Join Callback_Sent ON CallBack_Sent.LogID = Callback.LogID

WHERE Callback_Sent.LogID = '' AND CallBack.ProbClass = '209' AND CallBack.OpenClose = 'O'

Open in new window

0
 
LVL 2

Author Comment

by:xeroxcanada
ID: 33456947
Great guys That did the trick. I knew I was close but could not figure out why the "<>" not working.

Can one of you have a look at this and tell me if there is a better way of doing this Query.

select *
from [dbo].[CallBack_Sent]
 
where DATEDIFF(d, Date_Sent, getdate()) >= '6'
and DateReceived is NULL AND OpenClose = 'O' AND ProbClass = '209'

Thank You again
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33457933
Depending on size of your table you may want to define variable and set it once, so you do not calculate datediff for each row.

declare @lastDate datetime
select @lastDate = getdate()-6

....
where Date_Sent <= @lastDate
and DateReceived is NULL AND OpenClose = 'O' AND ProbClass = '209'
0
 
LVL 2

Author Closing Comment

by:xeroxcanada
ID: 33458431
Great Job..Thank You
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now