PostgreSQL 集合返回函數(shù)

2021-08-27 10:40 更新

本節(jié)描述那些可能返回多于一行的函數(shù)。目前這個類中被使用最廣泛的是級數(shù)生成函數(shù), 如表 9.61和表 9.62所述。其他更特殊的集合返回函數(shù)在本手冊的其他地方描述。 組合多集合返回函數(shù)的方法可見第 7.2.1.4 節(jié)

表 9.61. 系列生成函數(shù)

函數(shù)

描述

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生成一系列的值,步長為step。 step默認(rèn)為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

startstop生成一系列的值,步長為step。


當(dāng)step為正時,如果start大于stop則返回零行。 相反,當(dāng)step為負(fù)時,如果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)

-- 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);
   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)

表 9.62. 下標(biāo)生成函數(shù)

函數(shù)

描述

generate_subscripts ( array anyarray, dim integer ) → setof integer

生成一個包含給定數(shù)組第dim維度的有效下標(biāo)的序列。

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

生成一個包含給定數(shù)組第dim維度的有效下標(biāo)的序列。當(dāng)reverse為真時,以相反的順序返回序列。


generate_subscripts是一個快捷函數(shù),它為給定數(shù)組的指定維度生成一組合法的下標(biāo)。 對于不具有請求維度的數(shù)組返回零行,對于任何輸入為NULL數(shù)組也返回零行。下面是一些例子:

-- 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)

當(dāng)FROM子句中的函數(shù)以WITH ORDINALITY作為后綴時,將在函數(shù)的輸出列上附加一個bigint列,該列從1開始,函數(shù)輸出的每一行加1。 這在 unnest()等集合返回函數(shù)的情況下最有用。

-- 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)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號