Pick a random attribute from group in Redshift

后端 未结 4 1495
攒了一身酷
攒了一身酷 2021-01-23 04:37

I have a data set in the form.

id  |   attribute
-----------------
1   |   a
2   |   b
2   |   a
2   |   a
3   |   c

Desired output:

4条回答
  •  隐瞒了意图╮
    2021-01-23 05:06

    This solution, inspired by Masashi, is simpler and accomplishes selecting a random element from a group in Redshift.

    SELECT id, first_value as attribute 
    FROM(SELECT id, FIRST_VALUE(attribute) 
        OVER(PARTITION BY id ORDER BY random() 
        ROWS BETWEEN unbounded preceding AND unbounded following) 
        FROM dataset) 
    GROUP BY id, attribute ORDER BY id;
    

提交回复
热议问题