How can I find MAX with relational algebra?

前端 未结 7 1117
庸人自扰
庸人自扰 2020-11-27 11:44

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

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 12:26

    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

提交回复
热议问题