postgresql - How to select random trial with sql? (Bernoulli Trial) -
for each parent exists select 1 child row randomly. table structure looks this:
parent id child parent_id id rank
where there 1..n child records each parent , rank unique per parent going 1 n.
the output should like:
parent child rank --------+-------+------- 1 34 7 2 56 8 ...
with each parent producing 1 child row. (this serve basis bernoulli trials.)
(postgresql has random() function gives number between 0 , 1.)
not familiar @ postgresql, perhaps this?
select distinct on (parent_id) parent_id "parent", id "child", rank child c order parent_id, random()
Comments
Post a Comment