readme.tablefunc
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· TABLEFUNC 代码 · 共 643 行 · 第 1/2 页
TABLEFUNC
643 行
CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(IN text, OUT row_name TEXT, OUT category_1 FLOAT8, OUT category_2 FLOAT8, OUT category_3 FLOAT8, OUT category_4 FLOAT8, OUT category_5 FLOAT8) RETURNS setof record AS '$libdir/tablefunc','crosstab' LANGUAGE C STABLE STRICT;Example usagecreate table ct(id serial, rowclass text, rowid text, attribute text, value text);insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att1','val1');insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att2','val2');insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att3','val3');insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att4','val4');insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att1','val5');insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att2','val6');insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att3','val7');insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att4','val8');SELECT *FROM crosstab( 'select rowid, attribute, value from ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;', 3)AS ct(row_name text, category_1 text, category_2 text, category_3 text); row_name | category_1 | category_2 | category_3----------+------------+------------+------------ test1 | val2 | val3 | test2 | val6 | val7 |(2 rows)==================================================================Namecrosstab(text, text) - returns a set of row_name, extra, and category value columnsSynopsiscrosstab(text source_sql, text category_sql)Inputs source_sql A SQL statement which produces the source set of data. The SQL statement must return one row_name column, one category column, and one value column. It may also have one or more "extra" columns. The row_name column must be first. The category and value columns must be the last two columns, in that order. "extra" columns must be columns 2 through (N - 2), where N is the total number of columns. The "extra" columns are assumed to be the same for all rows with the same row_name. The values returned are copied from the first row with a given row_name and subsequent values of these columns are ignored until row_name changes. e.g. source_sql must produce a set something like: SELECT row_name, extra_col, cat, value FROM foo; row_name extra_col cat value ----------+------------+-----+--------- row1 extra1 cat1 val1 row1 extra1 cat2 val2 row1 extra1 cat4 val4 row2 extra2 cat1 val5 row2 extra2 cat2 val6 row2 extra2 cat3 val7 row2 extra2 cat4 val8 category_sql A SQL statement which produces the distinct set of categories. The SQL statement must return one category column only. category_sql must produce at least one result row or an error will be generated. category_sql must not produce duplicate categories or an error will be generated. e.g. SELECT DISTINCT cat FROM foo; cat ------- cat1 cat2 cat3 cat4Outputs Returns setof record, which must be defined with a column definition in the FROM clause of the SELECT statement, e.g.: SELECT * FROM crosstab(source_sql, cat_sql) AS ct(row_name text, extra text, cat1 text, cat2 text, cat3 text, cat4 text); the example crosstab function produces a set something like: <== values columns ==> row_name extra cat1 cat2 cat3 cat4 ---------+-------+------+------+------+------ row1 extra1 val1 val2 val4 row2 extra2 val5 val6 val7 val8Notes 1. source_sql must be ordered by row_name (column 1). 2. The number of values columns is determined at run-time. The column definition provided in the FROM clause must provide for the correct number of columns of the proper data types. 3. Missing values (i.e. not enough adjacent rows of same row_name to fill the number of result values columns) are filled in with nulls. 4. Extra values (i.e. source rows with category not found in category_sql result) are skipped. 5. Rows with a null row_name column are skipped. 6. You can create predefined functions to avoid having to write out the result column names/types in each query. See the examples for crosstab(text).Example usagecreate table cth(id serial, rowid text, rowdt timestamp, attribute text, val text);insert into cth values(DEFAULT,'test1','01 March 2003','temperature','42');insert into cth values(DEFAULT,'test1','01 March 2003','test_result','PASS');insert into cth values(DEFAULT,'test1','01 March 2003','volts','2.6987');insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53');insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL');insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003');insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234');SELECT * FROM crosstab( 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', 'SELECT DISTINCT attribute FROM cth ORDER BY 1')AS( rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); rowid | rowdt | temperature | test_result | test_startdate | volts -------+--------------------------+-------------+-------------+--------------------------+-------- test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987 test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234(2 rows)==================================================================Nameconnectby(text, text, text[, text], text, text, int[, text]) - returns a set representing a hierarchy (tree structure)Synopsisconnectby(text relname, text keyid_fld, text parent_keyid_fld [, text orderby_fld], text start_with, int max_depth [, text branch_delim])Inputs relname Name of the source relation keyid_fld Name of the key field parent_keyid_fld Name of the key_parent field orderby_fld If optional ordering of siblings is desired: Name of the field to order siblings start_with root value of the tree input as a text value regardless of keyid_fld type max_depth zero (0) for unlimited depth, otherwise restrict level to this depth branch_delim If optional branch value is desired, this string is used as the delimiter. When not provided, a default value of '~' is used for internal recursion detection only, and no "branch" field is returned.Outputs Returns setof record, which must defined with a column definition in the FROM clause of the SELECT statement, e.g.: SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text); - or - SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int); - or - SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos int); - or - SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos int); Notes 1. keyid and parent_keyid must be the same data type 2. The column definition *must* include a third column of type INT4 for the level value output 3. If the branch field is not desired, omit both the branch_delim input parameter *and* the branch field in the query column definition. Note that when branch_delim is not provided, a default value of '~' is used for branch_delim for internal recursion detection, even though the branch field is not returned. 4. If the branch field is desired, it must be the fourth column in the query column definition, and it must be type TEXT. 5. The parameters representing table and field names must include double quotes if the names are mixed-case or contain special characters. 6. If sorting of siblings is desired, the orderby_fld input parameter *and* a name for the resulting serial field (type INT32) in the query column definition must be given.Example usageCREATE TABLE connectby_tree(keyid text, parent_keyid text, pos int);INSERT INTO connectby_tree VALUES('row1',NULL, 0);INSERT INTO connectby_tree VALUES('row2','row1', 0);INSERT INTO connectby_tree VALUES('row3','row1', 0);INSERT INTO connectby_tree VALUES('row4','row2', 1);INSERT INTO connectby_tree VALUES('row5','row2', 0);INSERT INTO connectby_tree VALUES('row6','row4', 0);INSERT INTO connectby_tree VALUES('row7','row3', 0);INSERT INTO connectby_tree VALUES('row8','row6', 0);INSERT INTO connectby_tree VALUES('row9','row5', 0);-- with branch, without orderby_fldSELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text); keyid | parent_keyid | level | branch-------+--------------+-------+--------------------- row2 | | 0 | row2 row4 | row2 | 1 | row2~row4 row6 | row4 | 2 | row2~row4~row6 row8 | row6 | 3 | row2~row4~row6~row8 row5 | row2 | 1 | row2~row5 row9 | row5 | 2 | row2~row5~row9(6 rows)-- without branch, without orderby_fldSELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int); keyid | parent_keyid | level-------+--------------+------- row2 | | 0 row4 | row2 | 1 row6 | row4 | 2 row8 | row6 | 3 row5 | row2 | 1 row9 | row5 | 2(6 rows)-- with branch, with orderby_fld (notice that row5 comes before row4)SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos int) ORDER BY t.pos; keyid | parent_keyid | level | branch | pos -------+--------------+-------+---------------------+----- row2 | | 0 | row2 | 1 row5 | row2 | 1 | row2~row5 | 2 row9 | row5 | 2 | row2~row5~row9 | 3 row4 | row2 | 1 | row2~row4 | 4 row6 | row4 | 2 | row2~row4~row6 | 5 row8 | row6 | 3 | row2~row4~row6~row8 | 6(6 rows)-- without branch, with orderby_fld (notice that row5 comes before row4)SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos int) ORDER BY t.pos; keyid | parent_keyid | level | pos-------+--------------+-------+----- row2 | | 0 | 1 row5 | row2 | 1 | 2 row9 | row5 | 2 | 3 row4 | row2 | 1 | 4 row6 | row4 | 2 | 5 row8 | row6 | 3 | 6(6 rows)==================================================================-- Joe Conway
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?