| 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 tableDataCol1 DataCol2------------------------id1 id10id2 id10id3 id10id10 id10id4 id11id5 id11id6 id12what i want is to only select distinct id from this 2 column and display in one columnthe result should be as below:NewCol--------------id1id2id3id4id5id6id10id11id12 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-15 : 00:12:43
|
if sql 2005 you can do this alsoSELECT DISTINCT DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (Data FOR Values IN ([DataCol1],[DataCol2]))u |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
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 datacol1unionselect distinct field2from datacol2 |
 |
|
|
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 datacol1unionselect 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. |
 |
|
|
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 alsoSELECT DISTINCT DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (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" |
 |
|
|
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 alsoSELECT DISTINCT DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (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 |
 |
|
|
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 alsoSELECT DISTINCT DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (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
|
 |
|
|
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 DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u)tON t.Data=yourotherquerylinkingfield |
 |
|
|
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 DataFROM(SELECT DatCol1,DataCol2 FROM Table)mUNPIVOT (Data FOR [Values] IN ([DataCol1],[DataCol2]))u)tON t.Data=yourotherquerylinkingfield
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-15 : 04:45:02
|
welcome |
 |
|
|
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())) % 3000from master..spt_values as v1cross join master..spt_values as v2where v1.type = 'p' and v2.type = 'p'*/-- Tara 31% of batch, CPU 672, DURATION 743, READS 4230SELECT DISTINCT Col1 AS NewCol FROM tstPesoUNIONSELECT DISTINCT Col2 FROM tstPesooption (maxdop 1)-- Tara 2 31% of batch, CPU 719, DURATION 853, READS 4230SELECT Col1 AS NewCol FROM tstPesoUNIONSELECT Col2 FROM tstPesooption (maxdop 1)-- visakh 37% of batch, CPU 641, DURATION 759, READS 2115SELECT DISTINCT DataFROM(SELECT Col1,Col2 FROM tstpeso)mUNPIVOT (Data FOR [Values] IN ([Col1],[Col2]))uoption (maxdop 1) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|