如何用关系代数求 MAX?

发布于 2024-10-27 22:19:30 字数 28 浏览 1 评论 0 原文

使用数据库时,如何使用关系代数求 MAX?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

找个人就嫁了吧 2024-11-03 22:19:30

假设您有一个关系 A,具有单个属性“a”(减少一个更复杂的关系是关系代数中的一个简单任务,我确信您已经做到了这一点),所以现在您想要找到最大值A 中的值。

一种方法是找到 A 与其自身的叉积,请务必重命名“a”,以便您的新关系具有具有不同名称的属性。例如:(

将“a”重命名为“a1”)X(将“a”重命名为“a2”)

现在选择“a1”<; 'a2',结果关系将具有除最大值之外的所有值。要获得最大值,只需找到原始关系之间的差异:

(A x A) - (select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A))

然后使用 project 运算符减少到单个列,如 Tobi Lehman 在下面的评论中建议的那样。

用关系代数符号来写这个(如果我没记错的话)。请注意,最终重命名(即 ρ)只是为了得到与原始关系中具有相同名称的属性:

ρa/a1a1(( A x A) - σa1 < a2a1/a(A) x ρa2/a(A))))

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))))

画▽骨i 2024-11-03 22:19:30

只是我的两分钱,因为我今天试图自己解决这个问题。

假设我们有 A = 1,2,3

如果您使用,

A x A - (select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A))

您将不会获得单个最大值,而是获得两列,例如 1|1, 2|1,3|2,3|1,3|2,3|

3获得 3 的方法是

project(a)A - project(a1)((select 'a1' < 'a2') ((rename 'a' as 'a1')(A) x (rename 'a' as 'a2')(A)))

至少这是我在类似情况下必须做的。

希望它可以帮助某人

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

流绪微梦 2024-11-03 22:19:30

假设我们与属性 A 和值 1,2,3 存在关系,

A

1
2
3

所以现在..

用 A1 重命名

A1
1
2
3

项目 A 值并再次
项目 A 值并使用 A2 重命名,

A2
1
2
3

使用 A2 加入此项目,即 \join_{A2
所以 - 输出模式:(A2整数,A1整数)

A2<A1

1|2
1|3
2|3

总是听到A2值将小于A1,因为我们像这样加入a2

现在投影A2输出如下所示,

A2
1
2

现在与原始属性不同

A diff A2

A
1
2
3

 diff

A2
1
2

输出为 3 最大值

lets think we have a relation with an attribute A and values 1,2,3

A

1
2
3

so now..

project A values and rename with A1

A1
1
2
3

again
project A values and rename with A2

A2
1
2
3

join this with A2<A1 i.e \join_{A2<A1}
so the - Output schema: (A2 integer, A1 integer)

A2<A1

1|2
1|3
2|3

hear always A2 values will be less than A1 because we join like that(a2<a1)

now project A2 the output is like below

A2
1
2

now diff with original attribute

A diff A2

A
1
2
3

 diff

A2
1
2

Output is 3 maximum value

ぇ气 2024-11-03 22:19:30

我现在已经忘记了大部分关系代数语法。仅使用 SELECTPROJECTMINUSRENAME 的查询希望

SELECT v1.number
FROM values v1
MINUS
SELECT v1.number
FROM values v1 JOIN values v2 ON v2.number > v1.number

您能翻译!

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!

べ繥欢鉨o。 2024-11-03 22:19:30

我知道这已经很旧了,但这里有一个手写的公式,可能会很方便!

在此处输入图像描述

关系 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.

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.
如日中天 2024-11-03 22:19:30

找到最大值:

  • 策略:

    1. 找到那些不是MAXx

      • A 关系重命名为 d,以便我们可以将每个 A x 与所有其他关系进行比较。
    2. 使用set Difference来查找在前面的步骤中未找到的Ax

  • 查询是:
    输入图像描述这里

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

り繁华旳梦境 2024-11-03 22:19:30
 Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))
 Project x(A) - Project A.x
(Select A.x < d.x (A x Rename d(A)))
无边思念无边月 2024-11-03 22:19:30

关系代数(通过比较过滤)

最近这个问题出现在数据库模块中作为练习材料,当我四处寻找帮助时,我找不到任何好的答案。 “Md. Rezwanul Haque”的答案是正确的,但并没有真正解释,因为它依赖于先验知识(如果您不理解笛卡尔积)。

所以,这是我的解释的答案,希望这能让一些人更容易:

    TABLE: PEOPLE
PEOPLE.name PEOPLE.age
'Jack'      16
'Megan'     15
'Harry'     14
'Lilly'     16
'Michael'   8

这个想法是“收集你不想要的东西,并将其从你拥有的东西中删除;留下你想要的东西。” code>

第 1 步(构建要查询的表)

使用 SELECTION 进行过滤时,我们只能比较元组中的内容。这意味着我们需要将要与之比较的数据添加到这些元组中。

因此,我们需要将 PEOPLE 表与我们想要比较的数据合并。这可以使用x(笛卡尔积)运算符来完成,

如下所示:PEOPLE x PEOPLE但是我们不能这样做,因为结果表看起来像这样:

           TABLE: PEOPLE x PEOPLE
PEOPLE.name PEOPLE.age PEOPLE.name PEOPLE.age 
'Jack'      16         'Jack'      16

我们有重复的属性名称这意味着我们需要创建PEOPLE表的副本,该表具有我们可以引用的不同名称。否则,我们无法使用x笛卡尔积运算符,因为它要求所有属性在结果表中都是唯一的,您不能有两个PEOPLE.name属性。

这是我们使用 RENAME Operator 的地方,它看起来像这样:

PEOPLE ⨯ (ρ ALT (PEOPLE))

这里我所做的是使用 >笛卡尔积合并PEOPLEALT,其中ALTPEOPLE重命名ALT

这将为我们提供一个看起来有点像这样的表:(

          TABLE: PEOPLE x ALT
PEOPLE.name  PEOPLE.age ALT.name  ALT.age
'jack'       16         'jack'    16
'jack'       16         'megan'   15
'jack'       16         'harry'   14
'jack'       16         'lilly'   16
'jack'       16         'michael' 8

结果表为 (PEOPLE.size * PEOPLE.size) = 5*5,其中 size 是元组的数量)其中 的每个值PEOPLEALT 的每个值相对应

第 2 步(选择)

现在我们可以过滤所有值并获取我们不想要的值。因此,假设我只想要 PEOPLE 中最年长的人,这个问题可以改写为:Only who are not young to someone 所以我们只抓取那些比某人年轻的人。我们这样做是因为查询我们不想要的内容比查询我们想要的内容更容易

因此,我们的谓词将是:PEOPLE.age PEOPLE.age PEOPLE.age PEOPLE.age PEOPLE.age PEOPLE.age PEOPLE.age ALT.age,我们仅选择那些比某人年轻的人。

如果我们将 Predicate 反转为 PEOPLE.age > ALT.age 我们会得到一组不是最年长的人,但至少比一个人年长。这可以帮助我们获得最年轻的人

给我们一个像这样的SELECTION

(σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

这将产生一个像这样的表:

TABLE: (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

PEOPLE.age  PEOPLE.name  ALT.name  ALT.age 
'megan'     15           'jack'    16
'megan'     15           'lilly'   16
'harry'     14           'jack'    16
'harry'     14           'megan'   15
'harry'     14           'lilly'   16
'michael'   8            'jack'    16
'michael'   8            'megan'   15
'michael'   8            'harry'   14
'michael'   8            'lilly'   16

其中结果是比某人年轻的人,以及他们比谁年轻。然而我们的查询是:Only people who are not young to some 这与此完全相反。所以这不是我们的目标,我们需要做更多的事情。如果您这样做:

π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

这将为我们提供一个由 megan、harry 和 michael 组成的表格,该表格由以下内容组成:仅比某人年轻的人

步骤 3 (得到我们的最终表)

现在我们有一个表,其中包含仅比某人年轻的人,但我们想要的是仅不比某人年轻的人所以什么我们需要做的是从 PEOPLE 表中删除所有比某人年轻的人,只给我们不比某人年轻的人

因此,我们需要使用减法运算PEOPLE 表中删除这些元组。这为我们提供了最终查询:

PEOPLE - (π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE)))))

生成下表:

PEOPLE - (π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE)))))

PEOPLE.name PEOPLE.age
'jack'      16
'lilly'     16

其中 Jack 和 Lilly 是 PEOPLE 中唯一不比某人年轻的人。

Relational Algebra (Filtering through Comparison)

Recently had this question turn up as practice material in a Database module and when I was searching around for help I couldn't find any good answers. The answer by "Md. Rezwanul Haque" is correct but it's not really explained as it relies on prior knowledge (If you don't understand Cartesian Product that is).

So, here is the answer with my explanation hopefully this makes it easier for some:

    TABLE: PEOPLE
PEOPLE.name PEOPLE.age
'Jack'      16
'Megan'     15
'Harry'     14
'Lilly'     16
'Michael'   8

The idea is to "Collect what you don't want and remove it from what you have; leaving you with what you want."

Step 1 (Building a Table to Query)

When filtering using SELECTION we can only compare what is in the Tuple we have. This means we need to add to those tuples the data we want to compare it to.

So, we would need to merge our PEOPLE Table with the data we want to compare with. This can be done using the x (Cartesian Product) Operator

Something like this: PEOPLE x PEOPLE however we cannot do this as the resulting table would look something like this:

           TABLE: PEOPLE x PEOPLE
PEOPLE.name PEOPLE.age PEOPLE.name PEOPLE.age 
'Jack'      16         'Jack'      16

We have duplicate Attribute names this means that we need to create a Copy of the PEOPLE Table, one which has a different name that we can reference. Otherwise we cannot use the x Cartesian Product Operator as it requires that all attributes be unique in the resulting table, you can not have two PEOPLE.name attributes.

This is where we would use the RENAME Operator which would look something like this:

PEOPLE ⨯ (ρ ALT (PEOPLE))

Here what I've done is use the Cartesian Product to merge PEOPLE and ALT where ALT is PEOPLE renamed to ALT

This would give us a Table that looks a bit like this:

          TABLE: PEOPLE x ALT
PEOPLE.name  PEOPLE.age ALT.name  ALT.age
'jack'       16         'jack'    16
'jack'       16         'megan'   15
'jack'       16         'harry'   14
'jack'       16         'lilly'   16
'jack'       16         'michael' 8

(The resulting table is (PEOPLE.size * PEOPLE.size) = 5*5 where size is the number of tuples) Where every value of PEOPLE is put against every value of ALT

Step 2 (Selecting)

Now we can filter through all values and grab the ones we don't want. So let's say I only want the eldest people in PEOPLE this question can be reworded to: Only people who are not younger than someone so we grab only those who are younger than someone. We do this because it's easier to Query for what we don't want that what we do want.

So, our Predicate would be: PEOPLE.age < ALT.age where we are selecting only those who are younger than someone.

If we were to reverse the Predicate to PEOPLE.age > ALT.age we would get a mix of people who are not the eldest, but who are older than at least one person. This could help us obtain the person who is the youngest

Giving us a SELECTION like this:

(σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

This would produce a TABLE like this:

TABLE: (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

PEOPLE.age  PEOPLE.name  ALT.name  ALT.age 
'megan'     15           'jack'    16
'megan'     15           'lilly'   16
'harry'     14           'jack'    16
'harry'     14           'megan'   15
'harry'     14           'lilly'   16
'michael'   8            'jack'    16
'michael'   8            'megan'   15
'michael'   8            'harry'   14
'michael'   8            'lilly'   16

Where the results are people who are younger than someone, and who they're younger than. However our Query is: Only people who are not younger than someone which is the exact opposite of this. So this isn't our goal, we need to do a little bit more. If you were to do:

π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE))))

This would give us a table consisting of megan, harry, and michael this is a table consisting of: Only people who are younger than someone

Step 3 (Getting our final Table)

Now we have a Table that consists of Only people who are younger than someone but what we want is Only people who are not younger than someone so what we need to do is remove all of the people who are younger than someone from the PEOPLE Table to give us only those who are not younger than someone.

So we need to use the Subtraction Operation to remove those Tuples from our PEOPLE table. Which gives us our Final Query of:

PEOPLE - (π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE)))))

Which produces the following Table:

PEOPLE - (π PEOPLE.name PEOPLE.age (σ (PEOPLE.age < ALT.age) (PEOPLE x (ρ ALT (PEOPLE)))))

PEOPLE.name PEOPLE.age
'jack'      16
'lilly'     16

Where Jack and Lilly are the only people in PEOPLE who are NOT Younger than someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文