【问题标题】:SQL using MAX and COUNT使用 MAX 和 COUNT 的 SQL
【发布时间】:2014-05-26 14:37:52
【问题描述】:

我需要选择所有行数最多的东西。

我正在使用这个:

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 一起使用,但这并不能解决我的问题,因为如果它们具有相同的值,我需要选择所有最大值。

【问题讨论】:

  • 好像T是no table的别名!

标签: sql select count max


【解决方案1】:

Where 条件中使用Max

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多