在 Postgres 中为每个组选择任意行
在 Presto 中,有一个 任意() 聚合函数来选择给定组中的任意行。如果没有group by
子句,那么我可以使用distinct on
。使用 group by
时,每个选定的列都必须位于 group by
中或者是聚合列。例如:
| id | foo |
| 1 | 123 |
| 1 | 321 |
select id, arbitrary(foo), count(*)
from mytable
group by id
返回1也没关系, 123, 2
或 1, 321, 2
。像 min()
或 max()
这样的方法可以工作,但速度要慢得多。
Postgres 中是否存在类似 argumentary()
的东西?
In Presto, there's an arbitrary()
aggregate function to select any arbitrary row in a given group. If there's no group by
clause, then I can use distinct on
. With group by
, every selected column must be in the group by
or be an aggregated column. E.g.:
| id | foo |
| 1 | 123 |
| 1 | 321 |
select id, arbitrary(foo), count(*)
from mytable
group by id
It doesn't matter if it returns 1, 123, 2
or 1, 321, 2
. Something like min()
or max()
works, but it's a lot slower.
Does something like arbitrary()
exist in Postgres?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有明确提及
asc
、desc
,则无法保证所有顺序。因此在上面的查询中 foo 的出现是任意的。If not explicit mention
asc
,desc
all the order is not guaranteed. Therefore in the above query the foo's appearance is arbitrary.