如何获得每组的最大行数?

时间:2013-01-25 00:30:05

标签: mysql sql greatest-n-per-group

我有一个数据库,人们可以为不同地方的人提交投票。在某个时间,我想找出每个地方谁拥有最多选票。 (一个人可以在两个不同的地方投票)

这是我到目前为止的SQL:

SELECT placeId, userVotedId, cnt 
FROM 
    (SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
     FROM users as u, users_votes as uvo, places as p 
     WHERE u.userId = uvo.userVotedId 
       AND p.placeId = uvo.placeId 
     GROUP BY userVotedId, placeId) 
AS RESULT

给了我这个结果:

enter image description here

现在,这些是我真正想要的行:

enter image description here

我的查询中缺少什么,所以我可以得到这个?

  • 我想要每个地方一个结果。所以我应该只看到不同的placeIds,其中userVotedId得到的票数最多。

  • 如果出现平局,随机获胜者将会做到!

3 个答案:

答案 0 :(得分:3)

好像你还需要一个聚合。在MAX()值和cnt上使用GROUP BY placeId, userVotedId汇总:

SELECT placeId, userVotedId, max(cnt)
FROM 
(
  SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId
) AS RESULT
GROUP BY placeId, userVotedId

注意:我将您的查询更改为使用JOIN语法而不是表格之间的逗号。

编辑,根据您的评论,以下内容应该有效:

select total.uservotedid,
  total.placeid,
  total.cnt
from
(
  SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId
) total
inner join
(
  select max(cnt) Mx, placeid
  from
  (
    SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
    FROM users as u
    INNER JOIN users_votes as uvo
      ON u.userId = uvo.userVotedId 
    INNER JOIN places as p 
      ON p.placeId = uvo.placeId 
    GROUP BY userVotedId, placeId
  ) mx
  group by placeid
) src
  on total.placeid = src.placeid
  and total.cnt = src.mx

请参阅SQL Fiddle with Demo

结果是:

| USERVOTEDID | PLACEID | CNT |
-------------------------------
|          65 |      11 |   1 |
|          67 |      13 |   1 |
|          67 |      25 |   1 |
|          67 |      51 |   2 |

编辑#2,如果你想要一个平局就返回一个随机数,那么你可以使用用户变量:

select uservotedid,
  placeid, 
  cnt
from
(
  select total.uservotedid,
    total.placeid,
    total.cnt,
    @rownum := case when @prev = total.placeid then @rownum+1 else 1 end rownum,
    @prev := total.placeid pplaceid
  from
  (
    SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
    FROM users as u
    INNER JOIN users_votes as uvo
      ON u.userId = uvo.userVotedId 
    INNER JOIN places as p 
      ON p.placeId = uvo.placeId 
    GROUP BY userVotedId, placeId
  ) total
  inner join
  (
    select max(cnt) Mx, placeid
    from
    (
      SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
      FROM users as u
      INNER JOIN users_votes as uvo
        ON u.userId = uvo.userVotedId 
      INNER JOIN places as p 
        ON p.placeId = uvo.placeId 
      GROUP BY userVotedId, placeId
    ) mx
    group by placeid
  ) src
    on total.placeid = src.placeid
    and total.cnt = src.mx
  order by total.placeid, total.uservotedid
) src
where rownum = 1
order by placeid, uservotedid

请参阅SQL Fiddle with Demo

答案 1 :(得分:1)

SELECT placeId, userVotedId, MAX(cnt)
    FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) AS RESULT
    GROUP BY  placeId

答案 2 :(得分:1)

为简单起见,我调用了您的查询测试:

SELECT * 
FROM Test T JOIN (
SELECT t.placeId, Max(t.cnt) maxcnt
FROM Test t
GROUP BY t.placeId) T2 ON T.placeId = T2.placeId and T.cnt = T2.maxcnt

这是Fiddle

BTW - 测试=:

SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId
祝你好运。

- 编辑 - 根据要求,这是最终代码:

SELECT * 
FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) T JOIN (
SELECT t.placeId, Max(t.cnt) maxcnt
FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) t
GROUP BY t.placeId) T2 ON T.placeId = T2.placeId and T.cnt = T2.maxcnt