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
 Select 2 column data to display in one colmn

Author  Topic 

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-12-14 : 21:58:32
Hi everyone, i need a help.
I want to select two column data and display it to one column data with only distinct data.

below is the table


DataCol1 DataCol2
------------------------
id1 id10
id2 id10
id3 id10
id10 id10
id4 id11
id5 id11
id6 id12


what i want is to only select distinct id from this 2 column and display in one column

the result should be as below:

NewCol
--------------
id1
id2
id3
id4
id5
id6
id10
id11
id12

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-14 : 22:24:38
I'm sure that there is a more efficient way to do it, but here's what I came up with:

SELECT DISTINCT DataCol1 AS NewCol FROM Table1
UNION
SELECT DISTINCT DataCol2 FROM Table2

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 00:05:27
quote:
Originally posted by tkizer

I'm sure that there is a more efficient way to do it, but here's what I came up with:

SELECT DISTINCT DataCol1 AS NewCol FROM Table1
UNION
SELECT DISTINCT DataCol2 FROM Table2

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



i dont think distinct is required as UNION by itself takes distinct of values
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-15 : 00:09:12
Yep.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 00:12:43
if sql 2005 you can do this also

SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR Values IN ([DataCol1],[DataCol2]))u
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-15 : 00:14:59
But which is more efficient?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

onlyforme
Starting Member

25 Posts

Posted - 2008-12-15 : 01:28:19
Hi,
This will meet ur pblm ,but the only condition is both fields must be of same data type.
select distinct field1 from datacol1
union
select distinct field2from datacol2
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 01:32:33
quote:
Originally posted by onlyforme

Hi,
This will meet ur pblm ,but the only condition is both fields must be of same data type.
select distinct field1 from datacol1
union
select distinct field2from datacol2


this is not even syntactically correct. datacol1 and datacol2 are columns not table.
And even if you meant tables, how does this differ from what Tara posted?Don't repeat what others have suggested before.Read previous solutions before posting.
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-12-15 : 03:20:09
quote:
Originally posted by visakh16

if sql 2005 you can do this also

SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR Values IN ([DataCol1],[DataCol2]))u




Visakh16, thank your reply.
But when i try to execute your query it show me error.
Message is "Incorrect syntax near keyword VALUES"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 03:36:45
quote:
Originally posted by calvinkwoo3000

quote:
Originally posted by visakh16

if sql 2005 you can do this also

SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u




Visakh16, thank your reply.
But when i try to execute your query it show me error.
Message is "Incorrect syntax near keyword VALUES"


put square braces around values
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-12-15 : 04:06:20
Thank you, is work.
but from this query how to i inner join with other table?




quote:
Originally posted by visakh16

quote:
Originally posted by calvinkwoo3000

quote:
Originally posted by visakh16

if sql 2005 you can do this also

SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u




Visakh16, thank your reply.
But when i try to execute your query it show me error.
Message is "Incorrect syntax near keyword VALUES"


put square braces around values



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 04:11:23
something like...



Your Other query...
INNER JOIN
(
SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u
)t
ON t.Data=yourotherquerylinkingfield
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

98 Posts

Posted - 2008-12-15 : 04:42:56
Thanks, visakh16.

Thank your help a lot :)

quote:
Originally posted by visakh16

something like...



Your Other query...
INNER JOIN
(
SELECT DISTINCT Data
FROM
(SELECT DatCol1,DataCol2 FROM Table)m
UNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u
)t
ON t.Data=yourotherquerylinkingfield


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 04:45:02
welcome
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-15 : 05:05:32
quote:
Originally posted by tkizer

But which is more efficient?
Based on a 5 time average
/*
CREATE TABLE tstPeso (Col1 INT, Col2 INT)

insert tstPeso (col1, col2)
SELECT top 1000000 abs(checksum(newid())) % 3000, abs(checksum(newid())) % 3000
from master..spt_values as v1
cross join master..spt_values as v2
where v1.type = 'p' and v2.type = 'p'
*/
-- Tara 31% of batch, CPU 672, DURATION 743, READS 4230
SELECT DISTINCT Col1 AS NewCol FROM tstPeso
UNION
SELECT DISTINCT Col2 FROM tstPeso
option (maxdop 1)

-- Tara 2 31% of batch, CPU 719, DURATION 853, READS 4230
SELECT Col1 AS NewCol FROM tstPeso
UNION
SELECT Col2 FROM tstPeso
option (maxdop 1)

-- visakh 37% of batch, CPU 641, DURATION 759, READS 2115
SELECT DISTINCT Data
FROM
(SELECT Col1,Col2 FROM tstpeso)m
UNPIVOT (Data FOR [Values] IN ([Col1],[Col2]))u
option (maxdop 1)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -