简体   繁体   中英

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. I want to get a random record for each unique value in A column.

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

select distinct on (a) t.*
from mytable t
order by a, random();

You can do it with distinct on :

select distinct on (a) a, b, c, d
from test t;

Here is a Demo

With DISTINCT ON, You tell PostgreSQL to return a single row for each distinct group defined by the ON clause.

More about that subject here: https://www.geekytidbits.com/postgres-distinct-on/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM