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
 General SQL Server Forums
 New to SQL Server Programming
 MATCH DIFFERENT VALUES IN A TABLE

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-07-16 : 05:23:03
Hi there,

I have two different tables (Table_Jan, Table_feb) with the same columns.

I need to take from column_1 of Table_feb all the values that don't appear in column_1 table_jan.

That way, I can know, which values of column_1 have disappeared in Table_feb.

Any idea?

Thanks

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 05:33:30
SELECT * FROM TabFeb
WHERE Col1 NOT IN
(SELECT Col1 FROM TabJan)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 05:36:42
OR you can use it with joins

SELECT tf.* FROM TabFeb tf
LEFT OUTER JOIN TabJan tj
ON tf.COL1 = tj.COL1
WHERE TJ.COL1 IS NULL

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 05:38:11
Or you can use EXCEPT

SELECT * FROM TabFeb
EXCEPT
SELECT * FROM TabJan

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-07-16 : 06:00:52
Thanks so much,

Im trying to use:

SELECT company, plate, SUM (workers) FROM table_feb
WHERE id NOT IN
(SELECT id FROM table_jan) AND colour='b'
GROUP BY company
ORDER BY company

msg 8120, level 16, state 1, line 1


You know why? If I use other queries, is not recognizing the condition AND..

Many thanks
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-16 : 11:04:00
SELECT company, plate, SUM(workers)
FROM table_feb
WHERE id NOT IN ( SELECT id
FROM table_jan )
AND colour = 'b'
GROUP BY company, plate

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page
   

- Advertisement -