# F.40.tablefunc
这个tablefunc
模块包括返回表(即多行)的各种函数。这些函数本身很有用,并且可以作为编写返回多行的C函数的示例。
该模块被认为是“受信任的”,也就是说,它可以由拥有创造
当前数据库的权限。
# F.40.1.提供的功能
表F.31总结了tablefunc
单元
表F.31.tablefunc
功能
# F.40.1.1. 正常值
normal_rand(int numvals, float8 mean, float8 stddev) returns setof float8
正常值
产生一组正态分布的随机值(高斯分布)。
*纽瓦尔
是要从函数返回的值的数目。意思是
是值和的正态分布的平均值标准偏差
*是数值正态分布的标准偏差。
例如,此调用请求1000个平均值为5、标准偏差为3的值:
test=# SELECT * FROM normal_rand(1000, 5, 3);
normal_rand
#### F.40.1.2. `crosstab(text)`
[]()
交叉表(文本sql)交叉表(文本sql,int N)
The `crosstab` function is used to produce “pivot” displays, wherein data is listed across the page rather than down. For example, we might have data like
第11排第1排第12排第1排第13排。。。第2排val21第2排val22第2排val23.。。
which we wish to display like
行1 val11 val12 val13.。。row2 val21 val22 val23.。。
The `crosstab` function takes a text parameter that is an SQL query producing raw data formatted in the first way, and produces a table formatted in the second way.
The *`sql`* parameter is an SQL statement that produces the source set of data. This statement must return one `row_name` column, one `category` column, and one `value` column. *`N`* is an obsolete parameter, ignored if supplied (formerly this had to match the number of output value columns, but now that is determined by the calling query).
For example, the provided query might produce a set something like:
行名称cat值
# 笔记
另见[\crosstabview](app psql.html#app-psql-META-COMMANDS-crosstabview)
命令,该命令提供与交叉表()
.
# F.40.1.3. 交叉表*
N*(文本)
crosstabN(text sql)
这个交叉表*
N*
函数是如何为常规应用程序设置自定义包装的示例交叉表
函数,这样就不需要在调用中写出列名和类型选择
查询这个tablefunc
模块包括交叉表2
, 交叉表3
和交叉表4
,其输出行类型定义为
CREATE TYPE tablefunc_crosstab_N AS (
row_name TEXT,
category_1 TEXT,
category_2 TEXT,
.
.
.
category_N TEXT
);
因此,当输入查询生成时,可以直接使用这些函数行名
和价值
类型的列文本
,需要2、3或4个输出值列。在所有其他方面,他们的行为与上文针对将军的描述完全相同交叉表
作用
例如,上一节中给出的示例也适用于
SELECT *
FROM crosstab3(
'select rowid, attribute, value
from ct
where attribute = ''att2'' or attribute = ''att3''
order by 1,2');
这些功能主要用于说明目的。您可以根据基础的返回类型和函数创建自己的返回类型和函数交叉表()
作用有两种方法:
创建一个描述所需输出列的复合类型,类似于中的示例
contrib/tablefunc/tablefunc--1.0.sql
.然后定义一个唯一的函数名文本
参数和返回设置您的_类型_名称
,但链接到相同的底层交叉表
C函数。例如,如果源数据生成的行名称文本
,以及浮动8
,您需要5个值列:CREATE TYPE my_crosstab_float8_5_cols AS ( my_row_name text, my_category_1 float8, my_category_2 float8, my_category_3 float8, my_category_4 float8, my_category_5 float8 ); CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(text) RETURNS setof my_crosstab_float8_5_cols AS '$libdir/tablefunc','crosstab' LANGUAGE C STABLE STRICT;
使用
出来
参数来隐式定义返回类型。同样的例子也可以这样做:CREATE OR REPLACE FUNCTION crosstab_float8_5_cols( IN text, OUT my_row_name text, OUT my_category_1 float8, OUT my_category_2 float8, OUT my_category_3 float8, OUT my_category_4 float8, OUT my_category_5 float8) RETURNS setof record AS '$libdir/tablefunc','crosstab' LANGUAGE C STABLE STRICT;
# F.40.1.4. 交叉表(文本,文本)
crosstab(text source_sql, text category_sql)
单参数表的主要局限性交叉表
它对一个组中的所有值都一视同仁,将每个值插入第一个可用列。如果您希望值列对应于特定的数据类别,而某些组可能没有某些类别的数据,那么这就不太好用。双参数形式交叉表
通过提供与输出列对应的类别的显式列表来处理这种情况。
*source_sql
*是生成数据源集的SQL语句。此语句必须返回一个行名
一列类别
列和一列价值
柱它还可能有一个或多个“额外”列。这个行名
列必须位于第一位。这个类别
和价值
列必须是最后两列,按顺序排列。之间有任何列吗行名
和类别
被视为“额外”。对于具有相同属性的所有行,“额外”列应相同行名
价值
例如*source_sql
*可能会产生一组类似于:
SELECT row_name, extra_col, cat, value FROM foo ORDER BY 1;
row_name extra_col cat value
#### F.40.1.5. `connectby`
[]()
connectby(文本relname、文本keyid_fld、文本parent_keyid_fld[,文本排序依据,fld_],文本以开头,int max_depth[,文本分支,delim_])
The `connectby` function produces a display of hierarchical data that is stored in a table. The table must have a key field that uniquely identifies rows, and a parent-key field that references the parent (if any) of each row. `connectby` can display the sub-tree descending from any row.
[Table F.32](tablefunc.html#TABLEFUNC-CONNECTBY-PARAMETERS) explains the parameters.
**Table F.32. `connectby` Parameters**
| Parameter | Description |
|--------------------|--------------------------------------------------------|
| *`relname`* | Name of the source relation |
| *`keyid_fld`* | Name of the key field |
|*`parent_keyid_fld`*| Name of the parent-key field |
| *`orderby_fld`* | Name of the field to order siblings by (optional) |
| *`start_with`* | Key value of the row to start at |
| *`max_depth`* |Maximum depth to descend to, or zero for unlimited depth|
| *`branch_delim`* |String to separate keys with in branch output (optional)|
The key and parent-key fields can be any data type, but they must be the same type. Note that the *`start_with`* value must be entered as a text string, regardless of the type of the key field.
The `connectby` function is declared to return `setof record`, so the actual names and types of the output columns must be defined in the `FROM` clause of the calling `SELECT` statement, for example:
从connectby('connectby_tree','keyid','parent_keyid','pos','row2',0',~')中选择*作为t(keyid text,parent_keyid text,level int,branch text,pos int);
The first two output columns are used for the current row's key and its parent row's key; they must match the type of the table's key field. The third output column is the depth in the tree and must be of type `integer`. If a *`branch_delim`* parameter was given, the next output column is the branch display and must be of type `text`. Finally, if an *`orderby_fld`* parameter was given, the last output column is a serial number, and must be of type `integer`.
The “branch” output column shows the path of keys taken to reach the current row. The keys are separated by the specified *`branch_delim`* string. If no branch display is wanted, omit both the *`branch_delim`* parameter and the branch column in the output column list.
If the ordering of siblings of the same parent is important, include the *`orderby_fld`* parameter to specify which field to order siblings by. This field can be of any sortable data type. The output column list must include a final integer serial-number column, if and only if *`orderby_fld`* is specified.
The parameters representing table and field names are copied as-is into the SQL queries that `connectby` generates internally. Therefore, include double quotes if the names are mixed-case or contain special characters. You may also need to schema-qualify the table name.
In large tables, performance will be poor unless there is an index on the parent-key field.
It is important that the *`branch_delim`* string not appear in any key values, else `connectby` may incorrectly report an infinite-recursion error. Note that if *`branch_delim`* is not provided, a default value of `~` is used for recursion detection purposes.
Here is an example:
创建表connectby_树(keyid文本、parent_keyid文本、pos int);
在connectby_树中插入值('row1',NULL,0);插入connectby_树值('row2','row1',0);插入connectby_树值('row3','row1',0);在connectby_树中插入值('row4'、'row2',1);插入connectby_树值('row5','row2',0);插入connectby_树值('row6','row4',0);插入connectby_树值('row7','row3',0);插入connectby_树值('row8','row6',0);插入connectby_树值('row9','row5',0);
--对于branch,如果没有orderby_fld(结果的顺序不受保证),则从connectby('connectby_tree','keyid','parent_keyid','row2',0',~')中选择*作为t(keyid文本,parent_keyid文本,级别int,分支文本);密钥ID |父密钥ID |级别|分支
# F.40.2.作者
乔·康威