MySQL中函数组合的模糊性

时间:2015-06-23 09:10:25

标签: mysql group-by aggregate-functions

考虑简单的学生表,

mysql> select * from student;
+--------------+--------------+-------+
| student_name | joining_date | marks |
+--------------+--------------+-------+
| Anurag       | 2013-06-15   |    50 |
| Chandra      | 2014-07-12   |    65 |
| Dev          | 2014-03-25   |    80 |
| Gopal        | 2015-05-12   |    60 |
| Indra        | 2015-05-20   |    75 |
| Ram          | 2015-01-10   |    75 |
| Shyam        | 2015-01-10   |    50 |
+--------------+--------------+-------+
7 rows in set (0.00 sec)

我需要找到:

  

在哪一年,最大的学生是否加入了学校?

因此,它与max(count())类似group by。但由于SQL不允许max(count()),我找到了使用>= all的替代方法。

以下是查询和结果:

mysql> select extract(year from joining_date) from student group by extract(year from joining_date) having count(student_name) >= all(select count(student_name) from student group by joining_date);
+---------------------------------+
| extract(year from joining_date) |
+---------------------------------+
|                            2014 |
|                            2015 |
+---------------------------------+
2 rows in set (0.00 sec)

我必须只获得2015年,但它会产生两个元组。 当我从=中删除>= all时,我会收到所需的结果。但最大值必须大于所有元素吗?

对类似问题的另一个查询使用>=

考虑帐户表:

mysql> select * from account;
+----------------+-------------+---------+
| account_number | branch_name | balance |
+----------------+-------------+---------+
| A101           | Downtown    |     500 |
| A102           | Perryridge  |     400 |
| A201           | Brighton    |     900 |
| A215           | Mianus      |     700 |
| A217           | Brighton    |     750 |
| A222           | Redwood     |     700 |
| A305           | Round Hill  |     350 |
+----------------+-------------+---------+
7 rows in set (0.00 sec)
  

我需要确定平均余额最高的分支

mysql> select branch_name from account group by branch_name having avg(balance) >= all(select avg(balance) from account group by branch_name); 
+-------------+
| branch_name |
+-------------+
| Brighton    |
+-------------+
1 row in set (0.00 sec)

但在替换>=' by>'时我明白了,

mysql> select branch_name from account group by branch_name having avg(balance) > all(select avg(balance) from account group by branch_name); 
Empty set (0.00 sec)

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

最多学生年份

仅返回最高学生的第一年:

SELECT tmp.year
FROM (
   SELECT count(*) AS total, EXTRACT(year FROM joining_date) AS year
   FROM student
   GROUP BY year
   ORDER BY total DESC
) AS tmp
LIMIT 1;

以相同的最大学生回归所有年份:

SELECT tmp.year
FROM (
   SELECT count(*) AS total, EXTRACT(year FROM joining_date) AS year
   FROM student
   GROUP BY year
) AS tmp
WHERE tmp.total = (
   SELECT MAX(tmp.total)
   FROM (
      SELECT count(*) AS total
      FROM student
      GROUP BY EXTRACT(year FROM joining_date)
   ) AS tmp
);

我找到了通过重复使用子查询来优化这一点的方法,但发现没有任何东西不能使它更大。

最高平均余额

仅返回第一个找到平均值最高的分支:

SELECT branch_name
FROM account
GROUP BY branch_name
ORDER BY AVG(balance) DESC
LIMIT 1;

返回具有相同最高平均值的所有分支:

SELECT branch_name
FROM account
GROUP BY branch_name
HAVING AVG(balance) = (
   SELECT AVG(balance)
   FROM account
   GROUP BY branch_name
   ORDER BY AVG(balance) DESC
   LIMIT 1
);