Postgres random using setseed

I would like to add a column with a random number using setseedin a table.

Original table structure (test_input) col_a,col_b,col_c

Desired Conclusion (test_output) col_a, col_b, col_c, random_id

The following returns the same random_idfor all rows instead of another value in each row.

select col_a,col_b,col_c,setseed(0.5),(
     select random() from generate_series(1,100) limit 1
     ) as random_id 
from test_input

Could you help me modify a query that uses setseedand returns another random_idin each row?

+4
source share
1 answer

You have to use setseedin different ways. Also, in your example is incorrect generate_series(). You need to use something like:

select setseed(0.5);

select col_a,col_b,col_c, random() as random_id from test_input;

, , , :

ORDER BY, . ORDER BY , , .

:

select setseed(0.5);

select *, random() as random_id from (        
select col_a,col_b,col_c from test_input order by col_a, col_b, col_c) a;

, col_a, col_b, col_c . , .

+4

Source: https://habr.com/ru/post/1531613/


All Articles