finding max value among two table without using max function in relational algebra

后端 未结 2 1766
余生分开走
余生分开走 2021-01-29 07:02

Suppose I have two tables A{int m} and B{int m} and I have to find maximum m among two tables using relational algebra but I cannot us

相关标签:
2条回答
  • 2021-01-29 07:33

    Hmm, I'm puzzled why the question involves two tables. For the question as asked, I would just UNION the two (as StilesCrisis has done), then solve for a single table.

    So: how to find the maximum m in a table using only NatJOIN? This is a simplified version of finding the top node on a table that holds a hierarchy (think assembly/component explosions or org charts).

    The key idea is that we need to 'copy' the table into something with a different attribute name so that we can compare the tuples pair-wise. (And this will therefore use the degenerate form of NatJOIN aka cross-product). See example here How can I find MAX with relational algebra?

    A NOT MATCHING
    ((A x (A RENAME m AS mm)) WHERE m < mm)
    

    The subtrahend is all tuples with m less than some other tuples. The anti-join is all the tuples except those -- ie the MAX. (Using NOT MATCHING I think is both more understandable than MINUS, and doesn't need the relations to be UNION-compatible. It's roughly equivalent to SQL NOT EXISTS).)

    [I've used Tutorial D syntax, to avoid mucking about with greek letters.]

    0 讨论(0)
  • 2021-01-29 07:38
    SELECT M FROM (SELECT M FROM A UNION SELECT M FROM B) ORDER BY M DESC LIMIT 1
    

    This doesn't use MAX, just plain vanilla SQL.

    0 讨论(0)
提交回复
热议问题