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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 compare two columns and result in third column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ppatel112
Starting Member

19 Posts

Posted - 07/05/2012 :  19:43:55  Show Profile  Reply with Quote
Hi Guys,

i am facing a query where there are two columns in sql table called ['BATCH_URL_101-6896$'].

data1
500
501
502
504
505
507

data2
501
502
505
507
508
514

i need a sql query to compare data2 column to data1 column and put the results in third column data3

data3 will need to look like
data3
500
504
505

please advise.

regards,
parth

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/05/2012 :  20:03:20  Show Profile  Reply with Quote
What is the logic you want to use? First I thought you wanted values that are in data1 column, but not in data2 column. 500 and 504 match that criterion. But 505 is in both tables, yet you have that in your expected results.
Go to Top of Page

ppatel112
Starting Member

19 Posts

Posted - 07/05/2012 :  20:06:24  Show Profile  Reply with Quote
oops sorry not 505 only 500 and 504

please advise
Go to Top of Page

ppatel112
Starting Member

19 Posts

Posted - 07/05/2012 :  20:48:33  Show Profile  Reply with Quote
can someone help??
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/05/2012 :  21:51:30  Show Profile  Reply with Quote
One of these should work.
-- 1.
SELECT
	a.data1
FROM      
	['BATCH_URL_101-6896$'] a
	LEFT JOIN  ['BATCH_URL_101-6896$'] b ON
		a.data1 = b.data2
WHERE
	b.data2 IS NULL;

-- 2.
SELECT
	a.data1
FROM
	['BATCH_URL_101-6896$'] a
WHERE
	NOT EXISTS (SELECT * FROM  ['BATCH_URL_101-6896$'] b WHERE b.data2=a.data1);

Edited by - sunitabeck on 07/05/2012 21:51:49
Go to Top of Page

ppatel112
Starting Member

19 Posts

Posted - 07/05/2012 :  22:01:52  Show Profile  Reply with Quote
thanks the first one worked
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.07 seconds. Powered By: Snitz Forums 2000