@@ -57,11 +57,16 @@ Installation:
5757 but you can create additional crosstab functions per the instructions
5858 in the documentation below.
5959
60- crosstab(text sql, N int )
60+ crosstab(text sql)
6161 - returns a set of row_name plus N category value columns
6262 - requires anonymous composite type syntax in the FROM clause. See
6363 the instructions in the documentation below.
6464
65+ crosstab(text sql, N int)
66+ - obsolete version of crosstab()
67+ - the argument N is now ignored, since the number of value columns
68+ is always determined by the calling query
69+
6570 connectby(text relname, text keyid_fld, text parent_keyid_fld
6671 [, text orderby_fld], text start_with, int max_depth
6772 [, text branch_delim])
@@ -133,7 +138,7 @@ Inputs
133138
134139 A SQL statement which produces the source set of data. The SQL statement
135140 must return one row_name column, one category column, and one value
136- column.
141+ column. row_name and value must be of type text.
137142
138143 e.g. provided sql must produce a set something like:
139144
@@ -152,15 +157,15 @@ Outputs
152157
153158 Returns setof tablefunc_crosstab_N, which is defined by:
154159
155- CREATE VIEW tablefunc_crosstab_N AS
156- SELECT
157- ''::TEXT AS row_name,
158- ''::TEXT AS category_1,
159- ''::TEXT AS category_2,
160+ CREATE TYPE tablefunc_crosstab_N AS (
161+ row_name TEXT,
162+ category_1 TEXT,
163+ category_2 TEXT,
160164 .
161165 .
162166 .
163- ''::TEXT AS category_N;
167+ category_N TEXT
168+ );
164169
165170 for the default installed functions, where N is 2, 3, or 4.
166171
@@ -188,31 +193,9 @@ Notes
188193
189194 6. The installed defaults are for illustration purposes. You
190195 can create your own return types and functions based on the
191- crosstab() function of the installed library.
192-
193- The return type must have a first column that matches the data
194- type of the sql set used as its source. The subsequent category
195- columns must have the same data type as the value column of the
196- sql result set.
197-
198- Create a VIEW to define your return type, similar to the VIEWS
199- in the provided installation script. Then define a unique function
200- name accepting one text parameter and returning setof your_view_name.
201- For example, if your source data produces row_names that are TEXT,
202- and values that are FLOAT8, and you want 5 category columns:
196+ crosstab() function of the installed library. See below for
197+ details.
203198
204- CREATE VIEW my_crosstab_float8_5_cols AS
205- SELECT
206- ''::TEXT AS row_name,
207- 0::FLOAT8 AS category_1,
208- 0::FLOAT8 AS category_2,
209- 0::FLOAT8 AS category_3,
210- 0::FLOAT8 AS category_4,
211- 0::FLOAT8 AS category_5;
212-
213- CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(text)
214- RETURNS setof my_crosstab_float8_5_cols
215- AS '$libdir/tablefunc','crosstab' LANGUAGE 'c' STABLE STRICT;
216199
217200Example usage
218201
@@ -241,11 +224,12 @@ select * from crosstab3(
241224==================================================================
242225Name
243226
244- crosstab(text, int) - returns a set of row_name
245- plus N category value columns
227+ crosstab(text) - returns a set of row_names plus category value columns
246228
247229Synopsis
248230
231+ crosstab(text sql)
232+
249233crosstab(text sql, int N)
250234
251235Inputs
@@ -271,15 +255,16 @@ Inputs
271255
272256 N
273257
274- number of category value columns
258+ Obsolete argument; ignored if supplied (formerly this had to match
259+ the number of category columns determined by the calling query)
275260
276261Outputs
277262
278- Returns setof record, which must defined with a column definition
263+ Returns setof record, which must be defined with a column definition
279264 in the FROM clause of the SELECT statement, e.g.:
280265
281266 SELECT *
282- FROM crosstab(sql, 2 ) AS ct(row_name text, category_1 text, category_2 text);
267+ FROM crosstab(sql) AS ct(row_name text, category_1 text, category_2 text);
283268
284269 the example crosstab function produces a set something like:
285270 <== values columns ==>
@@ -292,9 +277,12 @@ Notes
292277
293278 1. The sql result must be ordered by 1,2.
294279
295- 2. The number of values columns is determined at run-time. The
296- column definition provided in the FROM clause must provide for
297- N + 1 columns of the proper data types.
280+ 2. The number of values columns is determined by the column definition
281+ provided in the FROM clause. The FROM clause must define one
282+ row_name column (of the same datatype as the first result column
283+ of the sql query) followed by N category columns (of the same
284+ datatype as the third result column of the sql query). You can
285+ set up as many category columns as you wish.
298286
299287 3. Missing values (i.e. not enough adjacent rows of same row_name to
300288 fill the number of result values columns) are filled in with nulls.
@@ -304,6 +292,44 @@ Notes
304292
305293 5. Rows with all nulls in the values columns are skipped.
306294
295+ 6. You can avoid always having to write out a FROM clause that defines the
296+ output columns by setting up a custom crosstab function that has
297+ the desired output row type wired into its definition.
298+
299+ There are two ways you can set up a custom crosstab function:
300+
301+ A. Create a composite type to define your return type, similar to the
302+ examples in the installation script. Then define a unique function
303+ name accepting one text parameter and returning setof your_type_name.
304+ For example, if your source data produces row_names that are TEXT,
305+ and values that are FLOAT8, and you want 5 category columns:
306+
307+ CREATE TYPE my_crosstab_float8_5_cols AS (
308+ row_name TEXT,
309+ category_1 FLOAT8,
310+ category_2 FLOAT8,
311+ category_3 FLOAT8,
312+ category_4 FLOAT8,
313+ category_5 FLOAT8
314+ );
315+
316+ CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(text)
317+ RETURNS setof my_crosstab_float8_5_cols
318+ AS '$libdir/tablefunc','crosstab' LANGUAGE 'c' STABLE STRICT;
319+
320+ B. Use OUT parameters to define the return type implicitly.
321+ The same example could also be done this way:
322+
323+ CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(IN text,
324+ OUT row_name TEXT,
325+ OUT category_1 FLOAT8,
326+ OUT category_2 FLOAT8,
327+ OUT category_3 FLOAT8,
328+ OUT category_4 FLOAT8,
329+ OUT category_5 FLOAT8)
330+ RETURNS setof record
331+ AS '$libdir/tablefunc','crosstab' LANGUAGE 'c' STABLE STRICT;
332+
307333
308334Example usage
309335
@@ -418,6 +444,10 @@ Notes
418444
419445 5. Rows with a null row_name column are skipped.
420446
447+ 6. You can create predefined functions to avoid having to write out
448+ the result column names/types in each query. See the examples
449+ for crosstab(text).
450+
421451
422452Example usage
423453
0 commit comments