问题
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