相当于关系代数中的 GroupBy 和having 子句

发布于 2024-12-09 09:48:03 字数 208 浏览 0 评论 0原文

我正在做一项作业,必须将 SQL 查询转换为关系代数查询。我在转换 group by 子句时陷入困境。

谁能告诉我如何用关系代数编写 group by 子句?

例如:

SELECT job, sal 
  FROM emp
 GROUP BY job
       ;

谢谢!

I was doing an assignment where I had to convert SQL queries into Relational Algebra queries. I got stuck in converting the group by clause.

Could anyone please tell me how the group by clause can be written in relational algebra?

e.g.:

SELECT job, sal 
  FROM emp
 GROUP BY job
       ;

Thanks!

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

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

发布评论

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

评论(2

虫児飞 2024-12-16 09:48:03

注意您想要获得工资总和,在 教程 D 中:

SUMMARIZE emp BY { job } ADD ( SUM ( sal ) AS total_sal )

注意聚合是不是关系运算符,因此不会构成关系代数的一部分。

至于HAVING,是历史异常吗?在 SQL-92 标准之前,不可能在 FROM 子句(也称为派生表)中编写 SELECT 表达式,即您必须在一个 中完成所有工作选择表达式。由于 SQL 严格的求值顺序,在对 WHERE 子句求值后,聚合值并不存在,即不可能应用基于聚合值的限制。 HAVING 的引入就是为了解决这个问题。

但即使有了HAVING,在引入派生表之前,SQL 在 Codd 方面仍然是不完整的。派生表呈现为 HAVING 冗余,但使用 HAVING 仍然很流行(如果 Stackoverflow 可以作为参考的话):人们似乎仍然喜欢使用单个 SELECTHAVING 相比,如果可能,SQL 前面提到的关于计算顺序的严格性(投影在 SELECT 表达式中最后执行)使得派生表的使用相当冗长。

Noting you want to get the sum of salary, in Tutorial D:

SUMMARIZE emp BY { job } ADD ( SUM ( sal ) AS total_sal )

Note aggregation is not a relational operator, hence will not form part of a relational algebra.

As for HAVING, is it a historical anomaly. Before the SQL-92 Standard, it was not possible to write SELECT expressions in the FROM clause (a.k.a derived tables) i.e. you had to do all work in one SELECT expression. Because of SQL's rigid evaluation order, the aggregate value doesn't come into existence after the WHERE clause has been evaluated i.e. it was impossible apply restriction based on aggregated values. HAVING was introduced to address this problem.

But even with HAVING, SQL remained relationally incomplete as regards Codd's until derived tables had been introduced. Derived tables rendered HAVINGredundant but using HAVING is still popular (if Stackoverflow is anything to go by): folk still seem to like to use a single SELECT where possible and SQL's aforementioned rigidity as regards evaluations order (projection is performed last in a SELECT expression) makes derived table usage quite verbose when compared to HAVING.

弃爱 2024-12-16 09:48:03

首先,您的查询是错误的,除非您使用聚合,否则您无法选择未分组的内容。我假设你想得到萨尔的总和。

工作 F 总和(sal),工作(emp)。

First of all your query is wrong you cannot select something that you did not group unless you use aggregation. I assume you want to get sum of the sal.

job F sum(sal), job(emp).

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