Solved

Select All form one if not in the other

Posted on 2010-08-17
5
342 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
[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
  • 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

617 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