Solved

Select All form one if not in the other

Posted on 2010-08-17
5
340 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

710 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