student student - 2 months ago 6
SQL Question

Get distinct on only one column on a multi column query


Let me try to explain my weird requirement.


On SQL Server 2008, say we have a
table
with
column1
&
column2
.

We need to get distinct values only for the
column1
. When we do
select distinct column1, column2 from table order by someColumn


Tried using joins, sub-queries etc but unable to find a solution.

Current output
column-zero
is duplicated


column-zero column-One column-Two column-Three
00074347303 401136 1144970 2015-09-21
30237890006 132384 855185 2015-11-06
50419020801 1917840 747677 2015-10-26
00074347303 300852 652090 2015-07-09
00074347303 307440 574437 2015-04-15
00074347303 307440 574437 2015-06-16
00074347303 131760 572546 2015-09-23
00074347303 150975 572485 2015-10-05
00074347303 148779 572485 2015-09-25
00074347303 148779 572485 2015-08-03
00074347303 99186 572485 2015-10-06
00074347303 99552 570289 2015-09-15
00074347303 99552 570289 2015-08-26
00074347303 153720 569313 2015-05-21
00074347303 258640 567117 2015-04-06
52544018876 81618 507398 2015-06-10
55513073001 134322 428830 2015-06-03
55513073001 134322 428830 2015-07-09
55513073001 137982 425109 2015-09-25
55513073001 137982 425109 2015-08-05


Expected output
column-zero
is distinct


column-zero column-One column-Two column-Three
00074347303 401136 1144970 2015-09-21
30237890006 132384 855185 2015-11-06
50419020801 1917840 747677 2015-10-26
00074347303 300852 652090 2015-07-09
52544018876 81618 507398 2015-06-10
55513073001 134322 428830 2015-06-03

swe swe
Answer

EDITED according to question-edit

WHAT you need is not distinct, but grouping

select col0, avg(col1), avg(col2), avg(col3) from table group by col0

Or try other aggregate function meeting your requirement: http://www.w3schools.com/sql/sql_functions.asp for mssql see: https://msdn.microsoft.com/en-us/library/ms173454.aspx

Comments