Solved

Select All form one if not in the other

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating SQL script using SQL data and SQL script 8 39
sql server computed columns 11 31
TSQL - How to declare table name 26 30
SQL server vNext 18 26
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 …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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