database 如何使用关系代数找到 MAX?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5493691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find MAX with relational algebra?
提问by Ifiok Idiang
Working with databases, how can I find MAX using relational algebra?
使用数据库,如何使用关系代数找到 MAX?
回答by Dan
Assuming you have a relation, A, with a single attribute, 'a' (reducing a more complex relation to this is a simple task in relational algebra, I'm sure you got this far), so now you want to find the maximum value in A.
假设你有一个关系 A,有一个单一的属性,“a”(减少一个更复杂的关系是关系代数中的一个简单任务,我相信你已经做到了这一点),所以现在你想找到最大值A 中的值
One way to do it is to find the cross product of A with itself, be sure to rename 'a' so your new relation has attributes with distinct names. for example:
一种方法是找到 A 与其自身的叉积,确保重命名“a”,以便您的新关系具有具有不同名称的属性。例如:
(rename 'a' as 'a1') X (rename 'a' as 'a2')
(将“a”重命名为“a1”) X(将“a”重命名为“a2”)
now select 'a1' < 'a2', the resulting relation will have all values except the maximum. To get the max simply find the difference between your original relation:
现在选择'a1' < 'a2',结果关系将具有除最大值之外的所有值。要获得最大值,只需找到原始关系之间的差异:
(A x A) - (select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A))
Then use the projectoperator to reduce down to a single column as Tobi Lehman suggests in the comment below.
然后project按照 Tobi Lehman 在下面评论中的建议,使用运算符将其缩减为单个列。
Writing this in relational algebra notation would be (if I remember correctly). Note the final rename (i.e. ρ) is just to end up with an attribute that has the same name as in the original relation:
用关系代数表示法写这个是(如果我没记错的话)。请注意,最终的重命名(即 ρ)只是为了得到一个与原始关系同名的属性:
ρa/a1(πa1((A x A) - σa1 < a2(ρa1/a(A) x ρa2/a(A))))
ρ a/a1(π a1((A x A) - σ a1 < a2(ρ a1/a(A) x ρ a2/a(A))))
回答by idipous
Just my two cents as I was trying to solve this today myself.
当我今天自己试图解决这个问题时,只需我的两美分。
Lets say we have A = 1,2,3
假设我们有 A = 1,2,3
If you use
如果你使用
A x A - (select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A))
you will not get the single max value rather two columns like 1|1, 2|1,3|2,3|1,3|2,3|3
你不会得到单个最大值,而是像 1|1, 2|1,3|2,3|1,3|2,3|3 这样的两列
the way to get just 3 is
只获得 3 的方法是
project(a)A - project(a1)((select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A)))
At least that is what I had to do in a similar situation.
至少这是我在类似情况下必须做的。
Hope it helps someone
希望它可以帮助某人
回答by bhv
lets think we have a relation with an attribute A and values 1,2,3
让我们认为我们与属性 A 和值 1,2,3 有关系
A
1
2
3
so now..
project A values and rename with A1
所以现在..
项目 A 值并用 A1 重命名
A1
1
2
3
again project A values and rename with A2
再次投影 A 值并用 A2 重命名
A2
1
2
3
join this with A2<A1i.e \join_{A2<A1}
so the - Output schema: (A2 integer, A1 integer)
加入A2<A1ie\join_{A2<A1}
所以 - 输出模式:(A2 整数,A1 整数)
A2<A1
1|2
1|3
2|3
hear always A2 values will be less than A1 because we joinlike that(a2<a1)
总是听到 A2 值将小于 A1 因为我们join喜欢那个(a2<a1)
now project A2 the output is like below
现在投影 A2 输出如下
A2
1
2
now diff with original attribute
现在与原始属性不同
A diff A2
A
1
2
3
diff
A2
1
2
Output is 3maximum value
输出为3最大值
回答by Martin Smith
I've forgotten most of the relational algebrasyntax now. A query just using SELECT, PROJECT, MINUSand RENAMEwould be
我现在已经忘记了大部分关系代数语法。查询只使用SELECT,PROJECT,MINUS和RENAME会
SELECT v1.number
FROM values v1
MINUS
SELECT v1.number
FROM values v1 JOIN values v2 ON v2.number > v1.number
Hopefully you can translate!
希望你能翻译!
回答by benscabbia
I know this is old, but here is a hand-written formula which might be handy!
我知道这是旧的,但这里有一个手写的公式,可能会很方便!


Relation A: 1,2,3,4
关系 A:1,2,3,4
1. First we want to PROJECT and RENAME relation A
2. We then to a THETA JOIN with the test a1<a2
3. We then PROJECT the result of the relation to give us a single set of values
a1: 1,2,3 (not max value since a1<a2)
4. We then apply the difference operator with the original relation so:
1,2,3,4 --- 1,2,3 returns 4
4 is the Max value.
回答by Md. Rezwanul Haque
Find the MAX:
找到最大值:
Strategy:
Find those
xthat are not theMAX.- Rename
Arelation asdso that we can compare eachAxwith all others.
- Rename
Use
set differenceto find thoseAxthat were not found in the earlier step.
回答by Raju
Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))

