I need to insert k random numbers (in the range 1:n) concatenated by | into a column of a table 'X', where n is the number of rows in table 'Y', in a PostgreSQL procedure.
To find the number of rows in the table 'Y'
select count(*) into n from Y
This will generate k random numbers in the range 1:n
SELECT num FROM GENERATE_SERIES (1, n) AS s(num) ORDER BY RANDOM() LIMIT k;
How do I concatenate the k integers with | and insert them into 'X'?
string_aggy- butselect count(*) into n from Yreturns the number of rows in that table. Two very different things. Also: storing delimited values in a column is almost always a really bad idea. A properly normalized model is much easier to maintain and to work withstring_aggI need to pass the output of the query as the first argument, would I have to use a temporary table to hold the results of the query for generating thekrandom numbers or is there any other optimal way?