SQL - HAVING 与 WHERE

发布于 2025-01-04 12:06:26 字数 765 浏览 1 评论 0原文

我有以下两个表:

1. Lecturers (LectID, Fname, Lname, degree).
2. Lecturers_Specialization (LectID, Expertise).

我想找到最专业化的讲师。 当我尝试这个时,它不起作用:

SELECT
  L.LectID, 
  Fname, 
  Lname 
FROM Lecturers L, 
     Lecturers_Specialization S
WHERE L.LectID = S.LectID
AND COUNT(S.Expertise) >= ALL (SELECT
  COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);

但是当我尝试这个时,它起作用:

SELECT
  L.LectID,
  Fname,
  Lname 
FROM Lecturers L,
     Lecturers_Specialization S
WHERE L.LectID = S.LectID
GROUP BY L.LectID,
         Fname,
         Lname 
HAVING COUNT(S.Expertise) >= ALL (SELECT
  COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID); 

原因是什么?谢谢。

I have the following two tables:

1. Lecturers (LectID, Fname, Lname, degree).
2. Lecturers_Specialization (LectID, Expertise).

I want to find the lecturer with the most Specialization.
When I try this, it is not working:

SELECT
  L.LectID, 
  Fname, 
  Lname 
FROM Lecturers L, 
     Lecturers_Specialization S
WHERE L.LectID = S.LectID
AND COUNT(S.Expertise) >= ALL (SELECT
  COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);

But when I try this, it works:

SELECT
  L.LectID,
  Fname,
  Lname 
FROM Lecturers L,
     Lecturers_Specialization S
WHERE L.LectID = S.LectID
GROUP BY L.LectID,
         Fname,
         Lname 
HAVING COUNT(S.Expertise) >= ALL (SELECT
  COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID); 

What is the reason? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

遗弃M 2025-01-11 12:06:26

WHERE 子句引入了各个行的条件; HAVING 子句引入了聚合的条件,即选择结果,其中单个结果(例如计数、平均值、最小值、最大值或总和)已从生成>多行。您的查询需要第二种条件(即聚合条件),因此 HAVING 可以正常工作。

根据经验,请在 GROUP BY 之前使用 WHERE,在 GROUP BY 之后使用 HAVING。这是一个相当原始的规则,但在 90% 以上的情况下都是有用的。

当您执行此操作时,您可能需要使用 ANSI 版本的连接重新编写查询:

SELECT  L.LectID, Fname, Lname
FROM Lecturers L
JOIN Lecturers_Specialization S ON L.LectID=S.LectID
GROUP BY L.LectID, Fname, Lname
HAVING COUNT(S.Expertise)>=ALL
(SELECT COUNT(Expertise) FROM Lecturers_Specialization GROUP BY LectID)

这将消除用作 theta 连接条件WHERE

WHERE clause introduces a condition on individual rows; HAVING clause introduces a condition on aggregations, i.e. results of selection where a single result, such as count, average, min, max, or sum, has been produced from multiple rows. Your query calls for a second kind of condition (i.e. a condition on an aggregation) hence HAVING works correctly.

As a rule of thumb, use WHERE before GROUP BY and HAVING after GROUP BY. It is a rather primitive rule, but it is useful in more than 90% of the cases.

While you're at it, you may want to re-write your query using ANSI version of the join:

SELECT  L.LectID, Fname, Lname
FROM Lecturers L
JOIN Lecturers_Specialization S ON L.LectID=S.LectID
GROUP BY L.LectID, Fname, Lname
HAVING COUNT(S.Expertise)>=ALL
(SELECT COUNT(Expertise) FROM Lecturers_Specialization GROUP BY LectID)

This would eliminate WHERE that was used as a theta join condition.

孤者何惧 2025-01-11 12:06:26

首先我们应该知道条款的执行顺序,即
来自>哪里>分组依据>具有>独特>选择>订购依据。
由于 WHERE 子句在 GROUP BY 子句之前执行,因此无法通过将 WHERE 应用于 GROUP BY 来过滤记录记录。

“HAVING 与 WHERE 子句相同,但应用于分组记录”。

首先,WHERE 子句根据条件获取记录,然后 GROUP BY strong>子句相应地对它们进行分组,然后HAVING子句根据having条件获取组记录。

First we should know the order of execution of Clauses i.e
FROM > WHERE > GROUP BY > HAVING > DISTINCT > SELECT > ORDER BY.
Since WHERE Clause gets executed before GROUP BY Clause the records cannot be filtered by applying WHERE to a GROUP BY applied records.

"HAVING is same as the WHERE clause but is applied on grouped records".

first the WHERE clause fetches the records based on the condition then the GROUP BY clause groups them accordingly and then the HAVING clause fetches the group records based on the having condition.

初雪 2025-01-11 12:06:26

HAVING 对聚合进行操作。由于 COUNT 是聚合函数,因此您不能在 WHERE 子句中使用它。

这里来自 MSDN 的一些汇总内容功能。

HAVING operates on aggregates. Since COUNT is an aggregate function, you can't use it in a WHERE clause.

Here's some reading from MSDN on aggregate functions.

飘然心甜 2025-01-11 12:06:26
  1. WHERE 子句可与 SELECTINSERTUPDATE 语句一起使用,而 HAVING 只能与 SELECT 语句一起使用。

  2. WHERE 在聚合之前过滤行 (GROUP BY),而 HAVING 在执行聚合之后过滤组。

  3. 聚合函数不能在 WHERE 子句中使用,除非它位于 HAVING 子句中包含的子查询中,而聚合函数可以在 HAVING 中使用> 条款。

来源

  1. WHERE clause can be used with SELECT, INSERT, and UPDATE statements, whereas HAVING can be used only with SELECT statement.

  2. WHERE filters rows before aggregation (GROUP BY), whereas HAVING filter groups after aggregations are performed.

  3. Aggregate function cannot be used in WHERE clause unless it is in a subquery contained in HAVING clause, whereas aggregate functions can be used in HAVING clause.

Source

极度宠爱 2025-01-11 12:06:26

在一个查询中没有看到两者的示例。所以这个例子可能会有所帮助。

  /**
INTERNATIONAL_ORDERS - table of orders by company by location by day
companyId, country, city, total, date
**/

SELECT country, city, sum(total) totalCityOrders 
FROM INTERNATIONAL_ORDERS with (nolock)
WHERE companyId = 884501253109
GROUP BY country, city
HAVING country = 'MX'
ORDER BY sum(total) DESC

这首先按 companyId 过滤表,然后将其分组(按国家和城市),最后将其过滤到仅墨西哥的城市聚合。聚合中不需要 companyId,但我们可以在使用 GROUP BY 之前使用 WHERE 过滤出我们想要的行。

Didn't see an example of both in one query. So this example might help.

  /**
INTERNATIONAL_ORDERS - table of orders by company by location by day
companyId, country, city, total, date
**/

SELECT country, city, sum(total) totalCityOrders 
FROM INTERNATIONAL_ORDERS with (nolock)
WHERE companyId = 884501253109
GROUP BY country, city
HAVING country = 'MX'
ORDER BY sum(total) DESC

This filters the table first by the companyId, then groups it (by country and city) and additionally filters it down to just city aggregations of Mexico. The companyId was not needed in the aggregation but we were able to use WHERE to filter out just the rows we wanted before using GROUP BY.

放赐 2025-01-11 12:06:26

您不能将 where 子句与聚合函数一起使用,因为 where 根据条件获取记录,它会逐条记录地进入表记录,然后根据我们给出的条件获取记录。所以到时候我们就可以不用where子句了。而having子句适用于我们在运行查询后最终得到的结果集。

示例查询:

select empName, sum(Bonus) 
from employees 
order by empName 
having sum(Bonus) > 5000;

这会将结果集存储在临时内存中,然后having 子句将执行其工作。所以我们在这里可以很容易地使用聚合函数。

You can not use where clause with aggregate functions because where fetch records on the basis of condition, it goes into table record by record and then fetch record on the basis of condition we have give. So that time we can not where clause. While having clause works on the resultSet which we finally get after running a query.

Example query:

select empName, sum(Bonus) 
from employees 
order by empName 
having sum(Bonus) > 5000;

This will store the resultSet in a temporary memory, then having clause will perform its work. So we can easily use aggregate functions here.

っ〆星空下的拥抱 2025-01-11 12:06:26

1.
我们可以将聚合函数与 HAVING 子句一起使用,而不是与 WHERE 子句一起使用,例如 min、max、avg。

2.
WHERE 子句逐元组消除记录元组
HAVING 子句从组的集合中消除整个组。

当您有数据组时,通常使用 HAVING,而当您有行数据时,则使用 WHERE。

1.
We can use aggregate function with HAVING clause not by WHERE clause e.g. min,max,avg.

2.
WHERE clause eliminates the record tuple by tuple
HAVING clause eliminates entire group from the collection of group

Mostly HAVING is used when you have groups of data and WHERE is used when you have data in rows.

眼睛会笑 2025-01-11 12:06:26

WHERE子句用于消除关系中的元组,HAVING子句用于消除关系中的组。

HAVING 子句用于聚合函数,例如
MIN,MAX,COUNT,SUM 。但始终在之前使用 GROUP BY 子句HAVING 子句可最大限度地减少错误。

WHERE clause is used to eliminate the tuples in a relation,and HAVING clause is used to eliminate the groups in a relation.

HAVING clause is used for aggregate functions such as
MIN,MAX,COUNT,SUM .But always use GROUP BY clause before HAVING clause to minimize the error.

栖迟 2025-01-11 12:06:26

WHEREHAVING 都用于过滤数据。
对于 WHERE 语句,数据过滤发生在您提取数据进行操作之前。

SELECT name, age 
FROM employees
WHERE age > 30;

这里,WHERE 子句在执行 SELECT 操作之前过滤行。

SELECT department, avg(age) avg_age
FROM employees
GROUP BY department
HAVING avg_age> 35;

HAVING 在执行 SELECT 操作后过滤数据。这里首先完成计算(聚合)操作,然后使用 HAVING 子句对结果应用过滤器。

Both WHERE and HAVING are used to filter data.
In case of a WHERE statement, data filtering happens before you pull the data for operation.

SELECT name, age 
FROM employees
WHERE age > 30;

Here the WHERE clause filters rows before the SELECT operation is performed.

SELECT department, avg(age) avg_age
FROM employees
GROUP BY department
HAVING avg_age> 35;

HAVING filters the data after the SELECT operation is performed. Here the operation of computing (aggregation) is done first and then a filter is applied to the result using a HAVING clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文