August 26, 2005 at 6:34 am
I need to compare the data in tbl_Reports with tbl_Names and find any unique data not matching tbl_Names.
So, tbl_Reports has changing data, and tbl_Names is constant.
August 26, 2005 at 6:51 am
Which fields need to be compared? Just an ID field, or several?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2005 at 7:21 am
Here's what I have so far. The problem is when I specify 'not in', it returns nothing.....
I have purposely place one unique name in tbl_Reports to test the query..
The query must return only unique names from tbl_Reports.FName that do NOT match any Fnames found in tbl_Names........
SELECT DISTINCT col_Fname
FROM tbl_Reports r JOIN tbl_Employees e
ON r.col_Fname != e.col_Fname
Where r.col_Fname not in (select col_Fname from tbl_Employees)
August 26, 2005 at 7:23 am
Check out exists and left join in the books online.
August 26, 2005 at 7:26 am
I don't think you want the JOIN.
SELECT DISTINCT col_Fname
FROM tbl_Reports
WHERE col_Fname NOT IN (SELECT col_Fname
FROM tbl_Employees)
August 26, 2005 at 8:04 am
Or this way:
SELECT DISTINCT col_Fname
FROM tbl_Reports r left join tbl_Employees e
ON r.col_Fname = e.col_Fname
WHERE e.col_Fname is null
Using != in a join is an advanced technique that will rarely be required. I have never needed it.
Regards
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2005 at 8:19 am
No, that did not work and I've been exploring the 'EXISTS' statement without success either.....
August 26, 2005 at 8:23 am
Noticed that col_Fname is ambiguous, so you need to qualify it:
SELECT DISTINCT r.col_Fname
FROM tbl_Reports r left join tbl_Employees e
ON r.col_Fname = e.col_Fname
WHERE e.col_Fname is null
If that doesn't work, there's something you're not telling us
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
August 26, 2005 at 8:27 am
That worked perfect........ Thank You............
August 26, 2005 at 8:28 am
Declare @Names table (name varchar(25) not null primary key clustered)
Declare @Peeps table (name varchar(25) not null primary key clustered)
Insert into @Names (name) values ('remi')
Insert into @Names (name) values ('sushila')
Insert into @Names (name) values ('phil')
Insert into @Peeps (name) values ('phil')
Insert into @Peeps (name) values ('remi')
Select N.name from @Names N where not exists (Select * from @Peeps P where N.name = P.name)
August 26, 2005 at 8:29 am
Check out which version is faster... that always change from one table to another... and in rarely is the fastest.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply