如何提取具有最大值的行?
我有两张桌子,想要执行以下操作。我从第一个表中提取满足给定条件的所有行。对于所有提取的行,我从第一列中获取一个值。对于每个值,我从第二个表中提取第一列中包含给定值的所有原始数据。从第二个表中提取的所有原始数据都必须以特定方式排序,我只想取第一行(根据排序标准)。这是我的问题,我不知道如何取排序标准最高值的一行。
添加
第一个表:
| aaa | Bob |
| bbb | Eva |
| ccc | Bob |
第二个表:
| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |
从第一个表中,我获取包含 Bob 的所有行。这些行中的第一列包含以下值:“aaa”和“ccc”。对于第二个表中的“aaa”,我有两行,我想采用最后一列中具有最大值的行。所以,我有 |aaa|342|2|
。我对“ccc”也是如此。我选择这一行|ccc|749|3|。最后我想根据最后一列的值对两行进行排序。
添加2
我刚刚意识到问题的本质如下。在给定的表中,我想用一个“代表性”行(在第三列中具有最大值)替换第一列中包含相同值的所有行。更详细地说,我想用这个替换这张表
| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |
:
| aaa | 342 | 2 |
| ccc | 749 | 3 |
I have two tables and want to do the following. From the first table I extract all rows that fulfill a given condition. For all extracted rows I take a value from the first column. For every value I extract all raws from the second table that contain the given value in the first column. All raws extracted from the second table have to be ordered in a specific way and I want to take only one row that is the first (according to the ordering criteria). This is my problem, I do not know how to take one row with the highest value of the ordering criteria.
ADDED
The first table:
| aaa | Bob |
| bbb | Eva |
| ccc | Bob |
The second table:
| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |
From the first table I take all rows containing Bob. The first column in these rows contain the following values: "aaa" and "ccc". For "aaa" in the second table I have two rows and I want to take those that has the maximal value in the last column. So, I have |aaa|342|2|
. The same I do with the "ccc". I take this row |ccc|749|3|. And finally I want to have the two rows ordered according to the value of the last column.
ADDED 2
I have just realized that the essence of the problem is as following. In a given table I want to replace all rows containing the same value in the first columns by one "representative" row (that has the maximal value in the third column). In more details, I want to replace this table:
| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |
By this one:
| aaa | 342 | 2 |
| ccc | 749 | 3 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有指定列名称,所以我编了它们。
t2.id
必须是唯一的。You did not specify column names, so i made them up.
t2.id
has to be unique.这可以使用相关子查询来实现,但性能会很糟糕。
This can be achieved using correlated subqueries but the performance will be abysmal.