Working with databases, how can I find MAX using relational algebra?
使用数据库,如何使用关系代数找到MAX ?
7 个解决方案
#1
65
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 project
operator to reduce down to a single column as Tobi Lehman suggests in the comment below.
然后,按照雷曼兄弟在下面评论中建议的那样,使用项目运营商将项目缩减为一栏。
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((x)——σa1 < a2(ρa1 /(A)xρa2 /(A))))
#2
30
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
希望它能帮助一些人
#3
18
I've forgotten most of the relational algebra syntax now. A query just using SELECT
, PROJECT
, MINUS
and RENAME
would 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!
希望你可以翻译!
#4
12
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
再次投影一个值并使用A2重命名
A2
1
2
3
join this with A2<A1
i.e \join_{A2<A1}
so the - Output schema: (A2 integer, A1 integer)
加上A2
A2<A1
1|2
1|3
2|3
hear always A2 values will be less than A1 because we join
like that(a2<a1
)
总是A2的值小于A1因为我们像这样连接(A2 < A1)
now project A2 the output is like below
现在项目A2的输出如下所示
A2
1
2
now diff with original attribute
现在,diff有了原来的属性。
A diff A2
A
1
2
3
diff
A2
1
2
Output is 3
which is maximum value
输出是3,这是最大值
Hi, i know some one have to help in editing, for better look
嗨,我知道有人需要帮忙编辑,为了更好的看。
#5
5
I know this is old, but here is a hand-written formula which might be handy!
我知道这是旧的,但这是一个手写的公式,可能方便!
Relation A: 1,2,3,4
关系: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.
#6
2
Find the MAX:
找到马克斯:
-
Strategy:
策略:
-
Find those
x
that are not theMAX
.找出那些不是最大值的x。
- Rename
A
relation asd
so that we can compare eachA
x
with all others. - 将一个关系重命名为d,以便我们可以将每个A x与其他所有的x进行比较。
- Rename
-
Use
set difference
to find thoseA
x
that were not found in the earlier step.使用set difference查找在前面步骤中没有找到的A x。
-
-
这个查询的方法是:
#7
1
Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))
#1
65
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 project
operator to reduce down to a single column as Tobi Lehman suggests in the comment below.
然后,按照雷曼兄弟在下面评论中建议的那样,使用项目运营商将项目缩减为一栏。
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((x)——σa1 < a2(ρa1 /(A)xρa2 /(A))))
#2
30
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
希望它能帮助一些人
#3
18
I've forgotten most of the relational algebra syntax now. A query just using SELECT
, PROJECT
, MINUS
and RENAME
would 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!
希望你可以翻译!
#4
12
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
再次投影一个值并使用A2重命名
A2
1
2
3
join this with A2<A1
i.e \join_{A2<A1}
so the - Output schema: (A2 integer, A1 integer)
加上A2
A2<A1
1|2
1|3
2|3
hear always A2 values will be less than A1 because we join
like that(a2<a1
)
总是A2的值小于A1因为我们像这样连接(A2 < A1)
now project A2 the output is like below
现在项目A2的输出如下所示
A2
1
2
now diff with original attribute
现在,diff有了原来的属性。
A diff A2
A
1
2
3
diff
A2
1
2
Output is 3
which is maximum value
输出是3,这是最大值
Hi, i know some one have to help in editing, for better look
嗨,我知道有人需要帮忙编辑,为了更好的看。
#5
5
I know this is old, but here is a hand-written formula which might be handy!
我知道这是旧的,但这是一个手写的公式,可能方便!
Relation A: 1,2,3,4
关系: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.
#6
2
Find the MAX:
找到马克斯:
-
Strategy:
策略:
-
Find those
x
that are not theMAX
.找出那些不是最大值的x。
- Rename
A
relation asd
so that we can compare eachA
x
with all others. - 将一个关系重命名为d,以便我们可以将每个A x与其他所有的x进行比较。
- Rename
-
Use
set difference
to find thoseA
x
that were not found in the earlier step.使用set difference查找在前面步骤中没有找到的A x。
-
-
这个查询的方法是:
#7
1
Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))