Query using aggregation and/or groups in relational algebra - count, max, min, etc

断了今生、忘了曾经 提交于 2021-02-08 05:13:07

问题


I have read much in textbooks and browsed a lot of pages on the internet but I can't understand how functions/operators like min, max, count, ... that aggregate over a relation/table or groups of tuples/rows in a relation/table are built with basic operations such as ∪ (union), ∩ (intersection), x (join), - (minus), π (projection), ....

Can anyone show me how to express these functions/operators with relational algebra?


回答1:


Computing functions in relation algebra are not fully included yet.

In relational algebra the aggregation operation over a schema (A1, A2, ... An) is written as follows:

G1, G2, ..., Gm g f1(A1'), f2(A2'), ..., fk(Ak') (r)

where each Aj', 1 ≤ j ≤ k, is one of the original attributes Ai, 1 ≤ i ≤ n.

The attributes preceding the g are grouping attributes, which function like a "group by" clause in SQL. Then there are an arbitrary number of aggregation functions applied to individual attributes. The operation is applied to an arbitrary relation r. The grouping attributes are optional, and if they are not supplied, the aggregation functions are applied across the entire relation to which the operation is applied.

Let's assume that we have a table named Account with three columns, namely Account_Number, Branch_Name and Balance. We wish to find the maximum balance of each branch. This is accomplished by Branch_NameGMax(Balance)(Account). To find the highest balance of all accounts regardless of branch, we could simply write GMax(Balance)(Account).



来源:https://stackoverflow.com/questions/15660545/query-using-aggregation-and-or-groups-in-relational-algebra-count-max-min-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!