Solved

Select All form one if not in the other

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

18 Experts available now in Live!

Get 1:1 Help Now