Working with databases, how can I find MAX using relational algebra?
相关问题
- NOT DISTINCT query in mySQL
- Flush single app django 1.9
- keeping one connection to DB or opening closing pe
- Mysql-installer showing error : Memoy could not be
- Android Room Fetch data with dynamic table name
相关文章
- Connection pooling vs persist connection mysqli
- Speed up sqlFetch()
- How Do I Seed My Database in the setupBeforeClass
- I set a MySQL column to “NOT NULL” but still I can
- Where in Django can I run startup code that requir
- Google OAuth 2.0 User id datatype for MYSQL
- Restore deleted records in PostgreSQL
- SQLSTATE[HY000] [2002] Permission denied
I've forgotten most of the relational algebra syntax now. A query just using
SELECT
,PROJECT
,MINUS
andRENAME
would beHopefully you can translate!
I know this is old, but here is a hand-written formula which might be handy!
Relation A: 1,2,3,4
Just my two cents as I was trying to solve this today myself.
Lets say we have A = 1,2,3
If you use
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
At least that is what I had to do in a similar situation.
Hope it helps someone
Find the MAX:
Strategy:
Find those
x
that are not theMAX
.A
relation asd
so that we can compare eachA
x
with all others.Use
set difference
to find thoseA
x
that were not found in the earlier step.The query is:
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:
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))))