PostgreSQL系列生成函数generate_series

定义

generate_series ( start integer, stop integer [, step integer ] ) → setof integer

generate_series ( start bigint, stop bigint [, step bigint ] ) → setof bigint

generate_series ( start numeric, stop numeric [, step numeric ] ) → setof numeric

从start到stop生成一系列的值,步长为step。 step默认为1。

generate_series ( start timestamp, stop timestamp, step interval ) → setof timestamp

generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval ) → setof timestamp with time zone

从start到stop生成一系列的值,步长为step。

示例

select generate_series(1,10);
select generate_series(1,10,2);

select generate_series(1.1,10.9);
select generate_series(1.1,10.9,0.4);

-- this example relies on the date-plus-integer operator:
SELECT current_date + s.a AS dates FROM generate_series(0,14,7) AS s(a);

SELECT * FROM generate_series('2008-03-01 00:00'::timestamp,
                              '2008-03-04 12:00', '10 hours');

select generate_series('2022-01-01 00:00:00'::timestamp,'2022-03-01 00:00:00'::timestamp, '1 d');

参考:http://postgres.cn/docs/13/functions-srf.html

你可能感兴趣的:(PostgreSQL,postgresql,数据库,database)