0

So I'm inserting a data for 3k columns but I want them to have random set of inserts to be inserted. For example:

('ATMOUTOFSERVICE','ATMBEINGSERVICE','NETWORKCONNECTIONLOST','BLACKSCREEN')

Is there any possible way that i can insert them in my columns randomly but just within those choices?

2
  • You can't have a table with 3000 columns. Did you mean 3000 rows? Commented Apr 3, 2020 at 8:40
  • Must it be a pure Postgres solution? If not, you could write a short Python, Java etc. program creating and executing INSERT statements. Commented Apr 3, 2020 at 8:42

1 Answer 1

3

You can use something like this:

insert into target_table (some_column)
select case (random() * 3 + 1)::int
          when 1 then 'ATMOUTOFSERVICE'
          when 2 then 'ATMBEINGSERVICE'
          when 3 then 'NETWORKCONNECTIONLOST'
          when 'BLACKSCREEN'
       end
from generate_series(1,...);

Or use an array:

insert into target_table (some_column)
select (array['ATMOUTOFSERVICE',
              'ATMBEINGSERVICE',
              'NETWORKCONNECTIONLOST',
              'BLACKSCREEN'])[(random() * 3 + 1)::int]
from generate_series(1, ...);
Sign up to request clarification or add additional context in comments.

3 Comments

can i use this on UPDATE query?
UPDATE dashboard.event SET event_description = ( select (array['CAM OUT OF SERVICE', 'CAM BEING SERVICE', 'NETWORK CONNECTION LOST', 'BLACKSCREEN', 'BNA DEVICE ERROR'])[(random() * 3 + 2)::int] from generate_series(1, 5)); i keep using this code but it won't update the null values
Get rid of the select and generate_series: SET event_description = (array['ATMOUTOFSERVICE', 'ATMBEINGSERVICE', 'NETWORKCONNECTIONLOST', 'BLACKSCREEN'])[(random() * 3 + 1)::int]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.