相当于关系代数中的 GroupBy 和having 子句
我正在做一项作业,必须将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意您想要获得工资总和,在 教程 D 中:
注意聚合是不是关系运算符,因此不会构成关系代数的一部分。
至于
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:
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 writeSELECT
expressions in theFROM
clause (a.k.a derived tables) i.e. you had to do all work in oneSELECT
expression. Because of SQL's rigid evaluation order, the aggregate value doesn't come into existence after theWHERE
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 renderedHAVING
redundant but usingHAVING
is still popular (if Stackoverflow is anything to go by): folk still seem to like to use a singleSELECT
where possible and SQL's aforementioned rigidity as regards evaluations order (projection is performed last in aSELECT
expression) makes derived table usage quite verbose when compared toHAVING
.首先,您的查询是错误的,除非您使用聚合,否则您无法选择未分组的内容。我假设你想得到萨尔的总和。
工作 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).