Narek Nersisyan
Narek Nersisyan

Reputation: 75

Is there an analogous function to generate_array in PostgreSQL?

Here's my query for reference:

with weekly_periods as (
  select ticket_id,
         start_time_in_minutes_from_week,
         raw_delta_in_minutes,
         schedule_id,
         week_number,
         greatest(0, start_time_in_minutes_from_week - week_number * (7*24*60)) as ticket_week_start_time,
         least(start_time_in_minutes_from_week + raw_delta_in_minutes - week_number * (7*24*60), (7*24*60)) as ticket_week_end_time
  from ticket_solved_time, unnest(generate_array(0, floor((start_time_in_minutes_from_week + raw_delta_in_minutes) / (7*24*60)), 1)) as week_number
)

I have tried generate_series and also the array functions, but I am not too familiar with the PostgreSQL syntax.

Upvotes: 0

Views: 431

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173151

You can use something like below in PostgreSQL to generate random integer array for example

select array_agg(round(random() * (max - min)) + min) 
from generate_series(0, elements)     

Note: generate_series function has many signatures that allow you to control what you will get out

Upvotes: 2

Related Questions