[英]How to select a random record for each group
I have a table like我有一张像
| A | B | C | D |
|--------|---|---|---|
| Value1 | x | x | x |
| Value1 | y | x | y |
| Value1 | x | x | x |
| .... |
| Value2 | x | x | x |
| Value2 | x | x | x |
| Value2 | x | x | x |
| .... |
| Value3 | x | x | x |
| Value3 | x | x | x |
| Value3 | x | x | x |
where A
column can have one value from a set.其中
A
列可以有一个集合中的一个值。 I want to get a random record for each unique value in A
column.我想为
A
列中的每个唯一值获取随机记录。
You can use window functions:您可以使用窗口函数:
select *
from (
select
t.*,
row_number() over(partition by a order by random()) rn
from mytable t
) t
where rn = 1
row_number()
assigns a random rank to each record within groups having the same a
; row_number()
为具有相同a
组中的每条记录分配一个随机排名; then, the outer query filters one record per group.然后,外部查询每组过滤一条记录。
Actually, since you are running Postgres, you could as well use distinct on
, which could give better performance (and a shorter syntax):实际上,由于您正在运行 Postgres,您也可以使用
distinct on
,这可以提供更好的性能(和更短的语法):
select distinct on (a) t.*
from mytable t
order by a, random();
You can do it with distinct on
:您可以使用
distinct on
做到这一点:
select distinct on (a) a, b, c, d
from test t;
With DISTINCT ON, You tell PostgreSQL to return a single row for each distinct group defined by the ON clause.
使用 DISTINCT ON,您告诉 PostgreSQL 为 ON 子句定义的每个不同组返回一行。
More about that subject here: https://www.geekytidbits.com/postgres-distinct-on/有关该主题的更多信息: https : //www.geekytidbits.com/postgres-distinct-on/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.