SQLite Handle Max()领带

时间:2017-04-10 18:17:12

标签: sql sqlite

我一直无法返回成功返回最大值的所有关系的查询。最大值来自内部查询。我的sql如下:

SELECT NAME, 
       Max(y.val) 
FROM   (SELECT NAME, 
               Count(*) AS val 
        FROM   numbers N 
        JOIN cards C 
          ON C.mid = N.mid 
        GROUP  BY NAME) AS y 

目前它只返回一个Max值,但如果存在平局,我需要它返回all。有人能用sqlite帮我这个吗?

1 个答案:

答案 0 :(得分:2)

这里的想法是隔离y并重新使用它来找到最大计数,然后匹配所有具有相同计数的行:

WITH y as (SELECT name, COUNT(*) as val 
          FROM Numbers N 
          JOIN Cards C on C.mid = N.mid
          GROUP BY name) 
SELECT name, val 
FROM y 
WHERE (SELECT MAX(val) from y)=val;