How can I find MAX with relational algebra?

2019-01-06 09:21发布

Working with databases, how can I find MAX using relational algebra?

7条回答
ゆ 、 Hurt°
2楼-- · 2019-01-06 09:56

 Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))
查看更多
forever°为你锁心
3楼-- · 2019-01-06 10:04

I've forgotten most of the relational algebra syntax now. A query just using SELECT, PROJECT, MINUS and RENAME would be

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楼-- · 2019-01-06 10:05

I know this is old, but here is a hand-written formula which might be handy!

enter image description here

Relation 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.
查看更多
ら.Afraid
5楼-- · 2019-01-06 10:14

Just my two cents as I was trying to solve this today myself.

Lets say we have 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

the way to get just 3 is

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

查看更多
Root(大扎)
6楼-- · 2019-01-06 10:14

Find the MAX:

  • Strategy:

    1. Find those x that are not the MAX.

      • Rename A relation as d so that we can compare each A x with all others.
    2. Use set difference to find those A x that were not found in the earlier step.

  • The query is: enter image description here

查看更多
老娘就宠你
7楼-- · 2019-01-06 10:15

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.

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:

(rename 'a' as 'a1') X (rename 'a' as '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:

(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/a1a1((A x A) - σa1 < a2a1/a(A) x ρa2/a(A))))

查看更多
登录 后发表回答