SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Statement to capture data from two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Johnph
Yak Posting Veteran

75 Posts

Posted - 07/25/2012 :  09:14:31  Show Profile  Reply with Quote
I have table 1 with columns "FIRST_NAME" and "LAST_NAME"
table 2 with columns "FIRST_NAME" and "MIDDLE_INT"

I need a query that can grab all "FIRST_NAME" in table 1 that ARE NOT in table 2 and include an ID column listing them as 1,2,3,4,5,6 etc.

Any help on this issue would be great.

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/25/2012 :  09:19:35  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
select id, first_name
from tbl1
where first_name not in (select first_name) from tbl2)

or maybe
select distinct first_name, id = row_number() over (order by first_name)
from tbl1
where first_name not in (select first_name) from tbl2)


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Johnph
Yak Posting Veteran

75 Posts

Posted - 07/25/2012 :  09:27:33  Show Profile  Reply with Quote
Hey, the second query worked perfectly. Thank you!
Go to Top of Page

Johnph
Yak Posting Veteran

75 Posts

Posted - 07/25/2012 :  09:35:06  Show Profile  Reply with Quote
Actually, is there a way to make the ID column varchar instead of BIGINT?
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/25/2012 :  10:21:44  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
select first_name, id = converrt(varchar(20),id)
from
(
select distinct first_name, id = row_number() over (order by first_name)
from tbl1
where first_name not in (select first_name) from tbl2)
) a

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000