@@ -989,6 +989,106 @@ testdb=>
989
989
</listitem>
990
990
</varlistentry>
991
991
992
+ <varlistentry>
993
+ <term><literal>\crosstabview [
994
+ <replaceable class="parameter">colV</replaceable>
995
+ <replaceable class="parameter">colH</replaceable>
996
+ [:<replaceable class="parameter">scolH</replaceable>]
997
+ [<replaceable class="parameter">colD</replaceable>]
998
+ ] </literal></term>
999
+ <listitem>
1000
+ <para>
1001
+ Execute the current query buffer (like <literal>\g</literal>) and shows
1002
+ the results inside a crosstab grid.
1003
+ The query must return at least three columns.
1004
+ The output column <replaceable class="parameter">colV</replaceable>
1005
+ becomes a vertical header
1006
+ and the output column <replaceable class="parameter">colH</replaceable>
1007
+ becomes a horizontal header, optionally sorted by ranking data obtained
1008
+ from <replaceable class="parameter">scolH</replaceable>.
1009
+ <replaceable class="parameter">colD</replaceable>
1010
+ is the output column to project into the grid. If this is not
1011
+ specified and there are exactly three columns in the result set,
1012
+ the column that isn't
1013
+ <replaceable class="parameter">colV</replaceable> nor
1014
+ <replaceable class="parameter">colH</replaceable>
1015
+ is displayed; if there are more columns, an error is thrown.
1016
+ </para>
1017
+
1018
+ <para>
1019
+ All columns can be refered to by their position (starting at 1), or by
1020
+ their name. Normal case folding and quoting rules apply on column
1021
+ names. By default,
1022
+ <replaceable class="parameter">colV</replaceable> corresponds to column 1
1023
+ and <replaceable class="parameter">colH</replaceable> to column 2.
1024
+ A query having only one output column cannot be viewed in crosstab, and
1025
+ <replaceable class="parameter">colH</replaceable> must differ from
1026
+ <replaceable class="parameter">colV</replaceable>.
1027
+ </para>
1028
+
1029
+ <para>
1030
+ The vertical header, displayed as the leftmost column,
1031
+ contains the deduplicated values found in
1032
+ column <replaceable class="parameter">colV</replaceable>, in the same
1033
+ order as in the query results.
1034
+ </para>
1035
+
1036
+ <para>
1037
+ The horizontal header, displayed as the first row,
1038
+ contains the deduplicated values found in
1039
+ column <replaceable class="parameter">colH</replaceable>, in
1040
+ the order of appearance in the query results.
1041
+ If specified, the optional <replaceable class="parameter">scolH</replaceable>
1042
+ argument refers to a column whose values should be integer numbers
1043
+ by which <replaceable class="parameter">colH</replaceable> will be sorted
1044
+ to be positioned in the horizontal header.
1045
+ </para>
1046
+
1047
+ <para>
1048
+ Inside the crosstab grid,
1049
+ given a query output with <literal>N</literal> columns
1050
+ (including <replaceable class="parameter">colV</replaceable> and
1051
+ <replaceable class="parameter">colH</replaceable>),
1052
+ for each distinct value <literal>x</literal> of
1053
+ <replaceable class="parameter">colH</replaceable>
1054
+ and each distinct value <literal>y</literal> of
1055
+ <replaceable class="parameter">colV</replaceable>,
1056
+ the contents of a cell located at the intersection
1057
+ <literal>(x,y)</literal> is determined by these rules:
1058
+ <itemizedlist>
1059
+ <listitem>
1060
+ <para>
1061
+ if there is no corresponding row in the query results such that the
1062
+ value for <replaceable class="parameter">colH</replaceable>
1063
+ is <literal>x</literal> and the value
1064
+ for <replaceable class="parameter">colV</replaceable>
1065
+ is <literal>y</literal>, the cell is empty.
1066
+ </para>
1067
+ </listitem>
1068
+
1069
+ <listitem>
1070
+ <para>
1071
+ if there is exactly one row such that the value
1072
+ for <replaceable class="parameter">colH</replaceable>
1073
+ is <literal>x</literal> and the value
1074
+ for <replaceable class="parameter">colV</replaceable>
1075
+ is <literal>y</literal>, then the <literal>colD</literal> column
1076
+ is displayed.
1077
+ </para>
1078
+ </listitem>
1079
+
1080
+ <listitem>
1081
+ <para>
1082
+ if there are several such rows, an error is thrown.
1083
+ </para>
1084
+ </listitem>
1085
+
1086
+ </itemizedlist>
1087
+ </para>
1088
+
1089
+ </listitem>
1090
+ </varlistentry>
1091
+
992
1092
<varlistentry>
993
1093
<term><literal>\d[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
994
1094
@@ -4070,6 +4170,47 @@ first | 4
4070
4170
second | four
4071
4171
</programlisting></para>
4072
4172
4173
+ <para>
4174
+ When suitable, query results can be shown in a crosstab representation
4175
+ with the \crosstabview command:
4176
+ <programlisting>
4177
+ testdb=> <userinput>SELECT first, second, first > 2 AS gt2 FROM my_table;</userinput>
4178
+ first | second | ge2
4179
+ -------+--------+-----
4180
+ 1 | one | f
4181
+ 2 | two | f
4182
+ 3 | three | t
4183
+ 4 | four | t
4184
+ (4 rows)
4185
+
4186
+ testdb=> <userinput>\crosstabview first second</userinput>
4187
+ first | one | two | three | four
4188
+ -------+-----+-----+-------+------
4189
+ 1 | f | | |
4190
+ 2 | | f | |
4191
+ 3 | | | t |
4192
+ 4 | | | | t
4193
+ (4 rows)
4194
+ </programlisting>
4195
+
4196
+ This second example shows a multiplication table with rows sorted in reverse
4197
+ numerical order and columns with an independant, ascending numerical order.
4198
+ <programlisting>
4199
+ testdb=> <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
4200
+ testdb(> <userinput>row_number() over(order by t2.first) AS ord</userinput>
4201
+ testdb(> <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
4202
+ testdb(> <userinput>\crosstabview A B:ord AxB</userinput>
4203
+ A | 101 | 102 | 103 | 104
4204
+ ---+-----+-----+-----+-----
4205
+ 4 | 404 | 408 | 412 | 416
4206
+ 3 | 303 | 306 | 309 | 312
4207
+ 2 | 202 | 204 | 206 | 208
4208
+ 1 | 101 | 102 | 103 | 104
4209
+ (4 rows)
4210
+ </programlisting>
4211
+
4212
+ </para>
4213
+
4073
4214
</refsect1>
4074
4215
4075
4216
</refentry>
0 commit comments