使用MAX和COUNT的SQL

时间:2014-04-12 11:04:54

标签: sql select count max

我需要选择具有最大行数的所有内容。

我正在使用它:

SELECT TMP.S, MAX(TMP.my_count)
FROM (
  SELECT T1.something S, COUNT(*) my_count
  FROM table T1, table2 T2
  WHERE T1.value = T2.value
  GROUP BY T1.something) TMP
GROUP BY TMP.S

然而,结果与此结果相同:

SELECT T1.something, COUNT(*) my_count
FROM table T1, table2 T2
WHERE T1.value = T2.value
GROUP BY T1.something

我可以将ORDER BY my_count DESCROWNUM = 1一起使用,但这并不能解决我的问题,因为如果它们具有相同的值,我需要选择所有最大值。

1 个答案:

答案 0 :(得分:0)

Max条件中使用Where

SELECT TMP.S, TMP.my_count
FROM (
      SELECT T1.something S, COUNT(*) my_count
      FROM table T1, table2 T2
      WHERE T1.value = T2.value
      GROUP BY T1.something 
      ) TMP
Where 
TMP.my_count = (select max(count(*)) from table1 T3 group by t3.something)