1

我有这张桌子:

id | day | count
----------------
 u   tue   1
 u   wed   4
 w   wed   5
 x   mon   5
 y   mon   5
 x   tue   2

我想返回每天计数最多的行。所以我想得到这张桌子:

id | day | count
----------------
 w   wed   5
 x   mon   5
 y   mon   5
 x   tue   2

这是我的 sql,但它没有给我正确的输出:

select id, day, MAX(count)
from Table
group by day

它给了我:

id | day | count
----------------
 w   wed   5
 y   mon   5
 x   tue   2
4

2 回答 2

4

您可以使用子查询:

select * from table 
  join (select day,max(count) as count from table group by day) as max_rec
    on table.day = max_rec.day and table.count = max_rec.count

sqlFiddle

于 2012-10-17T22:55:10.037 回答
2
select t1.* from
Table t1
JOIN
(
select day, max(count) count
from Table
group by day
)t2 ON t1.day=t2.day AND t1.count=t2.count
于 2012-10-17T22:56:17.320 回答