9.3 9.4 9.5 9.6 10 11 12 13 14 15 Current(16) 17
问题报告 纠错本页面
9.25. 集合返回函数
上一页 上一级 第 9 章 函数和操作符起始页 下一页

9.25. 集合返回函数 #

本节描述那些可能返回多于一行的函数。目前这个类中被使用最广泛的是级数生成函数, 如表 9.65表 9.66所述。其他更特殊的集合返回函数在本手册的其他地方描述。 组合多集合返回函数的方法可见第 7.2.1.4 节

表 9.65. 系列生成函数

函数

描述

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

startstop生成一系列的值,步长为stepstep默认为1。

generate_series ( start timestamp, stop timestamp, step interval ) → 一组时间戳

generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval [, timezone text ] ) → 一组带时区的时间戳

生成从startstop的一系列值, 步长为step。 在支持时区的形式中,白天时间和夏令时调整是根据 timezone参数指定的时区计算的, 如果省略该参数,则使用当前的TimeZone设置。


step为正时,如果start大于 stop,则返回零行。相反,当step 为负时,如果start小于stop,则 返回零行。如果任何输入为NULL,也会返回零行。若 step为零,则会报错。以下是一些示例:

SELECT * FROM generate_series(2,4);
 generate_series
-----------------
 2
 3
 4
(3 rows)
SELECT * FROM generate_series(5,1,-2);
 generate_series
-----------------
 5
 3
 1
(3 rows)
SELECT * FROM generate_series(4,3);
 generate_series
-----------------
(0 rows)
SELECT generate_series(1.1, 4, 1.3);
 generate_series
-----------------
 1.1
 2.4
 3.7
(3 rows)
-- 此示例依赖于日期加整数运算符:
SELECT current_date + s.a AS dates FROM generate_series(0,14,7) AS s(a);
 dates
------------
 2004年02月05日
 2004年02月12日
 2004年02月19日
(3 rows)
SELECT * FROM generate_series('2008-03-01 00:00'::timestamp,
 '2008-03-04 12:00', '10 hours');
 generate_series
---------------------
 2008年03月01日 00:00:00
 2008年03月01日 10:00:00
 2008年03月01日 20:00:00
 2008年03月02日 06:00:00
 2008年03月02日 16:00:00
 2008年03月03日 02:00:00
 2008年03月03日 12:00:00
 2008年03月03日 22:00:00
 2008年03月04日 08:00:00
(9 rows)
-- 此示例假设时区设置为UTC;注意夏令时的转换:
SELECT * FROM generate_series('2001-10-22 00:00 -04:00'::timestamptz,
 '2001-11-01 00:00 -05:00'::timestamptz,
 '1 day'::interval, 'America/New_York');
 generate_series
------------------------
 2001年10月22日 04:00:00+00
 2001年10月23日 04:00:00+00
 2001年10月24日 04:00:00+00
 2001年10月25日 04:00:00+00
 2001年10月26日 04:00:00+00
 2001年10月27日 04:00:00+00
 2001年10月28日 04:00:00+00
 2001年10月29日 05:00:00+00
 2001年10月30日 05:00:00+00
 2001年10月31日 05:00:00+00
 2001年11月01日 05:00:00+00
(11 rows)

表 9.66. 下标生成函数

函数

描述

generate_subscripts ( array anyarray, dim integer ) → setof integer

生成一个包含给定数组第dim维度的有效下标的序列。

generate_subscripts ( array anyarray, dim integer, reverse boolean ) → setof integer

生成一个包含给定数组第dim维度的有效下标的序列。当reverse为真时,以相反的顺序返回序列。


generate_subscripts是一个快捷函数,它为给定数组的指定维度生成一组合法的下标。 对于不具有请求维度的数组返回零行,对于任何输入为NULL数组也返回零行。下面是一些例子:

-- basic usage:
SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1) AS s;
 s
---
 1
 2
 3
 4
(4 rows)
-- presenting an array, the subscript and the subscripted
-- value requires a subquery:
SELECT * FROM arrays;
 a
--------------------
 {-1,-2}
 {100,200,300}
(2 rows)
SELECT a AS array, s AS subscript, a[s] AS value
FROM (SELECT generate_subscripts(a, 1) AS s, a FROM arrays) foo;
 array | subscript | value
---------------+-----------+-------
 {-1,-2} | 1 | -1
 {-1,-2} | 2 | -2
 {100,200,300} | 1 | 100
 {100,200,300} | 2 | 200
 {100,200,300} | 3 | 300
(5 rows)
-- unnest a 2D array:
CREATE OR REPLACE FUNCTION unnest2(anyarray)
RETURNS SETOF anyelement AS $$
select 1ドル[i][j]
 from generate_subscripts(1,1ドル) g1(i),
 generate_subscripts(1,2ドル) g2(j);
$$ LANGUAGE sql IMMUTABLE;
CREATE FUNCTION
SELECT * FROM unnest2(ARRAY[[1,2],[3,4]]);
 unnest2
---------
 1
 2
 3
 4
(4 rows)

FROM子句中的函数以WITH ORDINALITY作为后缀时,将在函数的输出列上附加一个bigint列,该列从1开始,函数输出的每一行加1。 这在 unnest()等集合返回函数的情况下最有用。

-- set returning function WITH ORDINALITY:
SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
 ls | n
-----------------+----
 pg_serial | 1
 pg_twophase | 2
 postmaster.opts | 3
 pg_notify | 4
 postgresql.conf | 5
 pg_tblspc | 6
 logfile | 7
 base | 8
 postmaster.pid | 9
 pg_ident.conf | 10
 global | 11
 pg_xact | 12
 pg_snapshots | 13
 pg_multixact | 14
 PG_VERSION | 15
 pg_wal | 16
 pg_hba.conf | 17
 pg_stat_tmp | 18
 pg_subtrans | 19
(19 rows)

上一页 上一级 下一页
9.24. 行和数组比较 起始页 9.26. 系统信息函数和运算符

AltStyle によって変換されたページ (->オリジナル) /