4 CREATE TABLE ctv_data (v, h, c, i, d) AS
6 ('v1','h2','foo', 3, '2015-04-01'::date),
7 ('v2','h1','bar', 3, '2015-01-02'),
8 ('v1','h0','baz', NULL, '2015-07-12'),
9 ('v0','h4','qux', 4, '2015-07-15'),
10 ('v0','h4','dbl', -3, '2014-12-15'),
11 ('v0',NULL,'qux', 5, '2014-07-15'),
12 ('v1','h2','quux',7, '2015-04-04');
13 -- make plans more stable
15 -- running \crosstabview after query uses query in buffer
16 SELECT v, EXTRACT(year FROM d), count(*)
21 ----+---------+-------
28 -- basic usage with 3 columns
37 -- ordered months in horizontal header, quoted column name
38 SELECT v, to_char(d, 'Mon') AS "month name", EXTRACT(month FROM d) AS num,
39 count(*) FROM ctv_data GROUP BY 1,2,3 ORDER BY 1
40 \crosstabview v "month name" 4 num
41 v | Jan | Apr | Jul | Dec
42 ----+-----+-----+-----+-----
48 -- ordered months in vertical header, ordered years in horizontal header
49 SELECT EXTRACT(year FROM d) AS year, to_char(d,'Mon') AS """month"" name",
50 EXTRACT(month FROM d) AS month,
51 format('sum=%s avg=%s', sum(i), avg(i)::numeric(2,1))
53 GROUP BY EXTRACT(year FROM d), to_char(d,'Mon'), EXTRACT(month FROM d)
55 \crosstabview """month"" name" year format year
56 "month" name | 2014 | 2015
57 --------------+-----------------+----------------
59 Apr | | sum=10 avg=5.0
60 Jul | sum=5 avg=5.0 | sum=4 avg=4.0
61 Dec | sum=-3 avg=-3.0 |
64 -- combine contents vertically into the same cell (V/H duplicates)
65 SELECT v, h, string_agg(c, E'\n') FROM ctv_data GROUP BY v, h ORDER BY 1,2,3
67 v | h4 | | h0 | h2 | h1
68 ----+-----+-----+-----+------+-----
76 -- horizontal ASC order from window function
77 SELECT v,h, string_agg(c, E'\n') AS c, row_number() OVER(ORDER BY h) AS r
78 FROM ctv_data GROUP BY v, h ORDER BY 1,3,2
80 v | h0 | h1 | h2 | h4 |
81 ----+-----+-----+------+-----+-----
89 -- horizontal DESC order from window function
90 SELECT v, h, string_agg(c, E'\n') AS c, row_number() OVER(ORDER BY h DESC) AS r
91 FROM ctv_data GROUP BY v, h ORDER BY 1,3,2
93 v | | h4 | h2 | h1 | h0
94 ----+-----+-----+------+-----+-----
102 -- horizontal ASC order from window function, NULLs pushed rightmost
103 SELECT v,h, string_agg(c, E'\n') AS c, row_number() OVER(ORDER BY h NULLS LAST) AS r
104 FROM ctv_data GROUP BY v, h ORDER BY 1,3,2
105 \crosstabview v h c r
106 v | h0 | h1 | h2 | h4 |
107 ----+-----+-----+------+-----+-----
110 v1 | baz | | foo +| |
115 -- only null, no column name, 2 columns: error
116 SELECT null,null \crosstabview
117 \crosstabview: query must return at least three columns
118 -- only null, no column name, 3 columns: works
119 SELECT null,null,null \crosstabview
127 SELECT v,h, string_agg(i::text, E'\n') AS i FROM ctv_data
128 GROUP BY v, h ORDER BY h,v
130 v | h0 | h1 | h2 | h4 | #null#
131 ----+--------+----+----+----+--------
132 v1 | #null# | | 3 +| |
140 -- refer to columns by position
141 SELECT v,h,string_agg(i::text, E'\n'), string_agg(c, E'\n')
142 FROM ctv_data GROUP BY v, h ORDER BY h,v
145 ----+------+-----+-----
155 -- refer to columns by positions and names mixed
156 SELECT v,h, string_agg(i::text, E'\n') AS i, string_agg(c, E'\n') AS c
157 FROM ctv_data GROUP BY v, h ORDER BY h,v
158 \crosstabview 1 "h" 4
159 v | h0 | h1 | h2 | h4 |
160 ----+-----+-----+------+-----+-----
161 v1 | baz | | foo +| |
168 -- refer to columns by quoted names, check downcasing of unquoted name
169 SELECT 1 as "22", 2 as b, 3 as "Foo"
170 \crosstabview "22" B "Foo"
176 -- error: bad column name
177 SELECT v,h,c,i FROM ctv_data
179 \crosstabview: column name not found: "j"
180 -- error: need to quote name
181 SELECT 1 as "22", 2 as b, 3 as "Foo"
182 \crosstabview 1 2 Foo
183 \crosstabview: column name not found: "foo"
184 -- error: need to not quote name
185 SELECT 1 as "22", 2 as b, 3 as "Foo"
186 \crosstabview 1 "B" "Foo"
187 \crosstabview: column name not found: "B"
188 -- error: bad column number
189 SELECT v,h,i,c FROM ctv_data
191 \crosstabview: column number 5 is out of range 1..4
192 -- error: same H and V columns
193 SELECT v,h,i,c FROM ctv_data
195 \crosstabview: vertical and horizontal headers must be different columns
196 -- error: too many columns
197 SELECT a,a,1 FROM generate_series(1,3000) AS a
199 \crosstabview: maximum number of columns (1600) exceeded
200 -- error: only one column
201 SELECT 1 \crosstabview
202 \crosstabview: query must return at least three columns
204 -- check error reporting (bug #14476)
205 CREATE TABLE ctv_data (x int, y int, v text);
206 INSERT INTO ctv_data SELECT 1, x, '*' || x FROM generate_series(1,10) x;
207 SELECT * FROM ctv_data \crosstabview
208 x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
209 ---+----+----+----+----+----+----+----+----+----+-----
210 1 | *1 | *2 | *3 | *4 | *5 | *6 | *7 | *8 | *9 | *10
213 INSERT INTO ctv_data VALUES (1, 10, '*'); -- duplicate data to cause error
214 SELECT * FROM ctv_data \crosstabview
215 \crosstabview: query result contains multiple data values for row "1", column "10"