dbTalk Databases Forums  

Distinct Column Level Output

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Distinct Column Level Output in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
raja
 
Posts: n/a

Default Distinct Column Level Output - 12-14-2009 , 06:54 AM






Hi,

I have a table having values like below :

col1,col2,col3,col4
a, b, c, d
a, b, c, e
a, b, c, f

I need the output as follows :
a,
b,
c,
d,
e,
f

can anyone, Please help me in this, How can i do that ?

Thanks.

With Regards,
Raja.

Reply With Quote
  #2  
Old   
pikes
 
Posts: n/a

Default Re: Distinct Column Level Output - 12-14-2009 , 07:57 AM






On Dec 14, 7:54*am, raja <dextersu... (AT) gmail (DOT) com> wrote:
Quote:
Hi,

I have a table having values like below :

col1,col2,col3,col4
a, * * *b, * * *c, * * *d
a, * * *b, * * *c, * * *e
a, * * *b, * * *c, * * *f

I need the output as follows :
a,
b,
c,
d,
e,
f

can anyone, Please help me in this, How can i do that ?

Thanks.

With Regards,
Raja.
One way I could see it done is like this:

select distinct tbl.c
from(
select col1 as c from tbl
union
select col2 as c from tbl
union
select col3 as c from tbl
union
select col4 as c from tbl) tbl

Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Distinct Column Level Output - 12-14-2009 , 09:24 AM



Here is one solution:

CREATE TABLE Foo (
keycol INT NOT NULL PRIMARY KEY,
col1 CHAR(1),
col2 CHAR(1),
col3 CHAR(1),
col4 CHAR(1));

INSERT INTO Foo VALUES(1, 'a', 'b', 'c', 'd');
INSERT INTO Foo VALUES(2, 'a', 'b', 'c', 'e');
INSERT INTO Foo VALUES(3, 'a', 'b', 'c', 'f');


SELECT DISTINCT value
FROM Foo
UNPIVOT
(value FOR col IN (col1, col2, col3, col4)) AS U;

/*

value
-----
a
b
c
d
e
f

*/

--
Plamen Ratchev
http://www.SQLStudio.com

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.