Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 compare two columns and result in third column

Author  Topic 

ppatel112
Starting Member

35 Posts

Posted - 2012-07-05 : 19:43:55
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-05 : 20:03:20
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

35 Posts

Posted - 2012-07-05 : 20:06:24
oops sorry not 505 only 500 and 504

please advise
Go to Top of Page

ppatel112
Starting Member

35 Posts

Posted - 2012-07-05 : 20:48:33
can someone help??
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-05 : 21:51:30
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);
Go to Top of Page

ppatel112
Starting Member

35 Posts

Posted - 2012-07-05 : 22:01:52
thanks the first one worked
Go to Top of Page
   

- Advertisement -