Unix Technical Forum

how to bring back the distinct values in single column from two tables

This is a discussion on how to bring back the distinct values in single column from two tables within the SQL Server forums, part of the Microsoft SQL Server category; --> 12.) Now you have two different tables - each with two columns. Table #1 Single Column 2 rows with ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:43 PM
TimG
 
Posts: n/a
Default how to bring back the distinct values in single column from two tables

12.) Now you have two different tables - each with two columns.

Table #1

Single Column

2 rows with a value equal to 1 and 2



Table #2

Single column

2 rows with a value equal to 2 and 4

Construct a statement returning in a single column all the values
contained, but not the common values.

This is another question that I got in an interview, I missed
it.......

Thanks,

Tim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:43 PM
David Portas
 
Posts: n/a
Default Re: how to bring back the distinct values in single column from two tables

CREATE TABLE T1 (x INTEGER PRIMARY KEY)
INSERT INTO T1 VALUES (1)
INSERT INTO T1 VALUES (2)

CREATE TABLE T2 (x INTEGER PRIMARY KEY)
INSERT INTO T2 VALUES (2)
INSERT INTO T2 VALUES (4)


Method 1:

SELECT COALESCE(T1.x, T2.x)
FROM T1
FULL JOIN T2
ON T1.x = T2.x
WHERE T1.x IS NULL OR T2.x IS NULL


Method 2:

SELECT x
FROM
(SELECT x
FROM T1
UNION ALL
SELECT x
FROM T2) AS T
GROUP BY x
HAVING COUNT(*)=1

--
David Portas
------------
Please reply only to the newsgroup
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:32 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com