SQL - 选择多行之间的最大值

时间:2013-09-16 18:51:57

标签: sql-server

我在不同的组中拥有相同类型的项目。我必须找到每个项目中最多的组。

首先,首先是数据:

Count_of_Items | Group_ID | Item_Type

15|01|A
35|02|A
25|03|A
 3|01|B
 5|04|B

等...

在这种情况下,组02的A类项目数最多(35),组04的B类项目数最多。

我试过

    select max(count_of_items), Group_ID , item_type 
      from foo
     group by Group_ID, item_type

但这没效果。

非常感谢您的帮助。

* 使用MS Sql Server 2005

1 个答案:

答案 0 :(得分:1)

试试这个,应该有效

select f.* 
from foo as f inner join
maxforGroup(
    select max(count_of_items) maxC, item_type 
    from foo
    group by item_type
) as m
on f.Count_of_Items=m.maxC and f.item_type =m.item_type
相关问题