总关系代数(最大值) [英] Aggregate Relational Algebra (Maximum)

查看:425
本文介绍了总关系代数(最大值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一项作业分配,要求进行选择以抽出一个元素,该元素包含一个与所有其他记录相比具有最大值的特定属性.我已经在线阅读了许多资料,这些资料引用了称为最大值"的聚合"关系代数函数,但是它们没有描述它如何使用基本运算符来工作.如何选择包含最大值的属性?

I am currently working on a homework assignment that requires a selection to occur that pulls out an element containing a specific attribute of maximum value compared to all other records. I've read a number of sources online that reference an "aggregate" relational algebra function called maximum, but they don't describe how it works using the basic operators. How does one select the attribute containing a maximum value?

推荐答案

仅使用基本运算符就可以很好地表达聚合函数.这是一件很整洁的事情.

You can very well express aggregate functions with only basic operators. It's a pretty neat thing.

假设我们有一个表T,我们想找到其值"字段的最大值.首先,我们应该将T的笛卡尔积与自身-或更确切地说,将其与副本T2进行比较.然后,我们选择T.value小于T2.value的行:这使我们将所有不需要的行都网了,其值小于某些其他行的值.为了获得最大值,我们应该从所有行集中减去这些不需要的行.就是这样.至少这是基本概念,我们还需要使用投影来正确确定尺寸.

Suppose we have a table T, and we'd like to find the maximum of its "value" field. First, we should take the cartesian product of T with itself -- or rather with a copy of itself, T2. Then we select the rows where T.value is smaller than T2.value: this nets us all the unwanted rows, whose value is less than the value of some other row. To get the maximal values, we should subtract these unwanted rows from the set of all rows. And that's it. At least that's the basic idea, we also need to use projections to get the dimensions right.

不幸的是,我不知道如何在此处插入Latex,但是使用关系代数表示法,可能是这样的:

Unfortunately I have no idea how to insert Latex here, but using relational algebra notation, it'd be something like this:

π(T.a1...Tan, T.value)(T)
    -
π(T.a1...Tan, T.value)(
    σ(T.value<T2.value)( ρ(T, T2) x T )
)

其中π是投影算子,-是集合差,σ是选择算子,ρ是重命名算子.

where π is the projection operator, - is the set difference, σ is the selection operator and ρ is the rename operator.

SQLishly:

SELECT T.* FROM T
    MINUS
SELECT T.* FROM T, T as T2 WHERE T.value<T2.value

更实际的是:

SELECT T.* FROM T LEFT JOIN T as T2 ON T.value<T2.value WHERE T2.value IS NULL

当然,所有这些主要只是学术上的兴趣,即表明关系代数有效.

Of course, all this is mostly only of academic interest, i.e. that it shows that the relational algebra works.

这篇关于总关系代数(最大值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆