SQL选择特定的DISTINCT行

时间:2015-11-30 14:12:08

标签: sql

我尝试提取一些数据,但事件列是截然不同的,但我特别想要只包含值列中包含最高数字的行,我认为它有些误捣乱使用DISTINCT和GROUP BY但我的sql知识目前是有限的,任何帮助都会很棒

enter image description here

4 个答案:

答案 0 :(得分:2)

试试这个:

SELECT [EVENT]
  ,MAX([Value]) 
FROM [MyTable] 
GROUP BY [EVENT]

答案 1 :(得分:1)

最简单的方法是使用子查询或row_number()

select event, value
from (select t.*, row_number() over (partition by event order by value desc) as seqnum
      from mytable t
     ) t
where seqnum = 1;

或:

select t.*
from mytable t
where t.value = (select max(t2.value) from mytable t2 where t2.event = t.event);

您应该注意命名列。 eventvalue可能是某些数据库中的保留字。

答案 2 :(得分:1)

SELECT [EVENT], MAX([Value]) FROM [My Table] GROUP BY [EVENT]怎么样?

答案 3 :(得分:0)

你走在正确的轨道上 select event, max(value) from test group by event