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.
| Author |
Topic |
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2008-01-06 : 22:23:04
|
| Is there a way in SQL 2005, so that i get the results using distinct in each of the columns in my select query.Select distinct Col1, distinct Col2, distinct Col3...from TableI know, the above syntax is wrong. But this is my requirement :(Help me with a query structure to achieve it.Thanks... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-06 : 22:49:56
|
[code]select distinct Col1, Col2, Col3from Table[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-06 : 23:07:42
|
You wont get distinct values of each columns by this method. It will only return the distinct values of the combination (Col1, Col2, Col3). To get individual distinct values you need to do strip out values out of table then join them together.DECLARE @DistCol1 TABLE(ID int IDENTITY(1,1),Col1 <datatype>)DECLARE @DistCol2 TABLE(ID int IDENTITY(1,1),Col2 <datatype>)DECLARE @DistCol3 TABLE(ID int IDENTITY(1,1),Col3 <datatype>)INSERT INTO @DistCol1 (Col1)SELECT DISTINCT Col1FROM YourTableINSERT INTO @DistCol2 (Col2)SELECT DISTINCT Col2FROM YourTableINSERT INTO @DistCol3 (Col3)SELECT DISTINCT Col3FROM YourTableSELECT t1.Col1,t2.Col2,t3.Col3FROM @DistCol1 t1LEFT OUTER JOIN @DistCol2 t2ON t2.ID=t1.IDLEFT OUTER JOIN @DistCol3 t3ON t3.ID=t1.ID I cant really understand the need for this requirement though. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-07 : 01:35:00
|
quote: Originally posted by sqlilliterate Is there a way in SQL 2005, so that i get the results using distinct in each of the columns in my select query.Select distinct Col1, distinct Col2, distinct Col3...from TableI know, the above syntax is wrong. But this is my requirement :(Help me with a query structure to achieve it.Thanks...
Post some sample data with expected resultMadhivananFailing to plan is Planning to fail |
 |
|
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2008-01-07 : 02:48:32
|
Here's some data...Col1 Col2 Col3----------------------A 1 ABB 1 ACC 0 ABA 1 ACA 0 BC A 0 BCA 1 ABB 0 ACC 1 ABC 0 ACB 1 BCB 0 BCA 1 BCC 0 ACD 1 AB Expected Result ...i.e. Distinct of each column n sorting is not required.Col1 Col2 Col3----------------------A 0 ABB 1 ACC NULL BCD NULL NULL |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-07 : 02:58:28
|
[code]DECLARE @sample TABLE( Col1 CHAR(1), Col2 int, Col3 CHAR(2))INSERT INTO @sampleSELECT 'A', 1, 'AB' UNION ALLSELECT 'B', 1, 'AC' UNION ALLSELECT 'C', 0, 'AB' UNION ALLSELECT 'A', 1, 'AC' UNION ALLSELECT 'A', 0, 'BC' UNION ALLSELECT 'A', 0, 'BC' UNION ALLSELECT 'A', 1, 'AB' UNION ALLSELECT 'B', 0, 'AC' UNION ALLSELECT 'C', 1, 'AB' UNION ALLSELECT 'C', 0, 'AC' UNION ALLSELECT 'B', 1, 'BC' UNION ALLSELECT 'B', 0, 'BC' UNION ALLSELECT 'A', 1, 'BC' UNION ALLSELECT 'C', 0, 'AC' UNION ALLSELECT 'D', 1, 'AB'SELECT Col1, Col2, Col3FROM ( SELECT row_no = row_number() OVER (ORDER BY Col1), Col1 FROM @sample GROUP BY Col1 ) c1FULL OUTER JOIN ( SELECT row_no = row_number() OVER (ORDER BY Col2), Col2 FROM @sample GROUP BY Col2 ) c2 ON c1.row_no = c2.row_noFULL OUTER JOIN ( SELECT row_no = row_number() OVER (ORDER BY Col3), Col3 FROM @sample GROUP BY Col3 ) c3 ON c1.row_no = c3.row_no/*Col1 Col2 Col3 ---- ----------- ---- A 0 ABB 1 ACC NULL BCD NULL NULL(4 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|