代数关系 sql GROUP BY SORT BY ORDER BY
Algebra Relational sql GROUP BY SORT BY ORDER BY
我想知道代数关系中 GROUP BY、SORT BY 和 ORDER BY 的等价物是什么?
这在关系代数中是不可能的,但人们一直在为这些操作创建一些“扩展”(注:在原文中,部分文本被写为下标)。
GROUP BY,根据《数据库系统基础知识》一书(Elmasri,Navathe 2011 年第 6 版):
Another type of request that cannot be expressed in the basic relational algebra is to
specify mathematical aggregate functions on collections of values from the database.
...
We can define an AGGREGATE FUNCTION operation, using the symbol ℑ (pronounced
script F)7, to specify these types of requests as follows:
<grouping attributes> ℑ <function list> (R)
where <grouping attributes> is a list of attributes of the relation specified in R, and <function list> is a list of (<function> <attribute>) pairs. In each such pair,
<function> is one of the allowed functions—such as SUM, AVERAGE, MAXIMUM,
MINIMUM,COUNT—and <attribute> is an attribute of the relation specified by R. The resulting relation has the grouping attributes plus one attribute for each element in the function list.
ORDER BY(排序依据),John L. Donaldson's lecture notes*(不再可用):
Since a relation is a set (or a bag), there is no ordering defined for a relation. That is, two relations are the same if they contain the same tuples, irrespective of ordering. However, a user frequently wants the output of a query to be listed in some particular order. We can define an additional operator τ which sorts a relation if we are willing to allow an operator whose output is not a relation, but an ordered list of tuples.
For example, the expression
τLastName,FirstName(Student)
generates a list of all the Student tuples, ordered by LastName (as the primary sort key) then FirstName (as a secondary sort key). (The secondary sort key is used only if two tuples agree on the primary sort key. A sorting operation can list any number of sort keys, from most significant to least significant.)
*John L. Donaldson(名誉教授)课程 CSCI 311 数据库系统的讲义 欧柏林计算机科学学院。参考 2015 年。检查 2022 年,不再可用。
您可以使用投影 π 对您想要的列进行分组 table 而无需聚合(PROJECT 操作删除任何重复的元组)
如下:
π c1,c2,c3 (R)
其中 c1、c2、c3 是列(属性),R 是 table(关系)
根据this SQL to relational algebra converter tool,我们有:
SELECT agents.agent_code, agents.agent_name, SUM(orders.advance_amount)
FROM agents, orders
WHERE agents.agent_code = orders.agent_code
GROUP BY agents.agent_code, agents.agent_name
ORDER BY agents.agent_code
写在函数中有点像:
τ agents.agent_code
γ agent_code, agent_name, SUM(advance_amount)
σ agents.agent_code = orders.agent_code (agents × orders)
图表如下:
我想知道代数关系中 GROUP BY、SORT BY 和 ORDER BY 的等价物是什么?
这在关系代数中是不可能的,但人们一直在为这些操作创建一些“扩展”(注:在原文中,部分文本被写为下标)。
GROUP BY,根据《数据库系统基础知识》一书(Elmasri,Navathe 2011 年第 6 版):
Another type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.
...
We can define an AGGREGATE FUNCTION operation, using the symbol ℑ (pronounced script F)7, to specify these types of requests as follows:
<grouping attributes> ℑ <function list> (R)
where <grouping attributes> is a list of attributes of the relation specified in R, and <function list> is a list of (<function> <attribute>) pairs. In each such pair, <function> is one of the allowed functions—such as SUM, AVERAGE, MAXIMUM, MINIMUM,COUNT—and <attribute> is an attribute of the relation specified by R. The resulting relation has the grouping attributes plus one attribute for each element in the function list.
ORDER BY(排序依据),John L. Donaldson's lecture notes*(不再可用):
Since a relation is a set (or a bag), there is no ordering defined for a relation. That is, two relations are the same if they contain the same tuples, irrespective of ordering. However, a user frequently wants the output of a query to be listed in some particular order. We can define an additional operator τ which sorts a relation if we are willing to allow an operator whose output is not a relation, but an ordered list of tuples.
For example, the expression
τLastName,FirstName(Student)
generates a list of all the Student tuples, ordered by LastName (as the primary sort key) then FirstName (as a secondary sort key). (The secondary sort key is used only if two tuples agree on the primary sort key. A sorting operation can list any number of sort keys, from most significant to least significant.)
*John L. Donaldson(名誉教授)课程 CSCI 311 数据库系统的讲义 欧柏林计算机科学学院。参考 2015 年。检查 2022 年,不再可用。
您可以使用投影 π 对您想要的列进行分组 table 而无需聚合(PROJECT 操作删除任何重复的元组) 如下:
π c1,c2,c3 (R)
其中 c1、c2、c3 是列(属性),R 是 table(关系)
根据this SQL to relational algebra converter tool,我们有:
SELECT agents.agent_code, agents.agent_name, SUM(orders.advance_amount)
FROM agents, orders
WHERE agents.agent_code = orders.agent_code
GROUP BY agents.agent_code, agents.agent_name
ORDER BY agents.agent_code
写在函数中有点像:
τ agents.agent_code
γ agent_code, agent_name, SUM(advance_amount)
σ agents.agent_code = orders.agent_code (agents × orders)
图表如下: