选择随机行每个组(Select random row for each group)

2019-08-31 08:43发布

我有一个这样的表

ID    ATTRIBUTE
 1    A
 1    A
 1    B
 1    C
 2    B
 2    C
 2    C
 3    A
 3    B
 3    C

我想选择一个为每个ID随机属性。 因此,结果可能会是这样的(虽然这只是众多选项之一

ATTRIBUTE
B
C
C

这是我对这个问题的尝试

SELECT
  "ATTRIBUTE"
FROM
  (
  SELECT
    "ID",
    "ATTRIBUTE",
    row_number() OVER (PARTITION BY "ID" ORDER BY random()) rownum
  FROM
    table
  ) shuffled
WHERE
  rownum = 1

但是,我不知道这是否是一个很好的解决方案,因为我需要介绍的行数,这是一个有点麻烦。

你有一个更好的?

Answer 1:

select distinct on (id) id, attribute
from like_this
order by id, random()

如果你只需要在属性栏:

select distinct on (id) attribute
from like_this
order by id, random()

请注意,您仍然需要通过命令id首先因为它是一列distinct on

如果你只是想在不同的属性:

select distinct attribute
from (
    select distinct on (id) attribute
    from like_this
    order by id, random()
) s


Answer 2:

把一个大的随机数在每个记录(ID)的前部和各组内选择具有最低随机数的纪录。

$ cat test.txt
\N  1   a
\N  2   b
\N  2   c
\N  2   d
\N  3   e
\N  4   f


$ mysql

USE test;
DROP TABLE test;
CREATE TABLE test (id0 INT NOT NULL AUTO_INCREMENT, id VARCHAR(1),  attribute VARCHAR(1), PRIMARY KEY (id0));
LOAD DATA LOCAL INFILE '~/mysql/test.txt' INTO TABLE test FIELDS TERMINATED BY '\t';

DROP TABLE rtest;
CREATE TABLE rtest (random INT(8), id0 VARCHAR(1), id VARCHAR(1),  attribute VARCHAR(1),  PRIMARY KEY (id, random));

INSERT INTO rtest
SELECT CAST(1000000. * rand() AS INT) AS random, test.* FROM test;

SELECT rtest.* FROM rtest,
(SELECT id, min(random) AS random FROM rtest GROUP BY id) AS sample WHERE rtest.random=sample.random AND rtest.id=sample.id;


文章来源: Select random row for each group