在多列查询上仅对一列进行区分

时间:2016-09-08 12:35:25

标签: sql sql-server join distinct

  

让我试着解释我的奇怪要求。

在SQL Server 2008上,假设我们tablecolumn1& column2

我们只需要为column1获取不同的值。当我们select distinct column1, column2 from table order by someColumn

尝试使用连接,子查询等但无法找到解决方案。

当前输出column-zero重复

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

预期输出column-zero是不同的

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

2 个答案:

答案 0 :(得分:2)

根据问题编辑

进行编辑

您需要的不是分明,而是分组

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

或尝试其他符合您要求的汇总功能:http://www.w3schools.com/sql/sql_functions.asp 对于mssql,请参阅:https://msdn.microsoft.com/en-us/library/ms173454.aspx

答案 1 :(得分:1)

尝试此查询:

select
distinct on (exe.column1) exe.column1,exe.column2
from
(
select
column1,
column2
from table 
order by someColumn
) exe