1
CREATE TEMP TABLE pivot(
gid SERIAL,
zoom smallint NOT NULL,
day timestamp with time zone NOT NULL,
point integer NOT NULL
);
INSERT DATA
INSERT INTO pivot(zoom, day, point) VALUES(6,'2015-10-01',21);
INSERT INTO pivot(zoom, day, point) VALUES(7,'2015-10-01',43);
INSERT INTO pivot(zoom, day, point) VALUES(8,'2015-10-01',18);
INSERT INTO pivot(zoom, day, point) VALUES(9,'2015-10-01',14);
INSERT INTO pivot(zoom, day, point) VALUES(10,'2015-10-01',23);
INSERT INTO pivot(zoom, day, point) VALUES(11,'2015-10-01',54);
INSERT INTO pivot(zoom, day, point) VALUES(6,'2015-10-02',657);
INSERT INTO pivot(zoom, day, point) VALUES(7,'2015-10-02',432);
INSERT INTO pivot(zoom, day, point) VALUES(8,'2015-10-02',421);
INSERT INTO pivot(zoom, day, point) VALUES(9,'2015-10-02',432);
INSERT INTO pivot(zoom, day, point) VALUES(10,'2015-10-02',454);
INSERT INTO pivot(zoom, day, point) VALUES(11,'2015-10-02',654);
すべてが今まで動作するかどうかを確認できます一時テーブルを作成:
SELECT zoom, day, point
FROM pivot
ORDER BY 1,2;
結果:
zoom | day | point
------+------------------------+-------
6 | 2015-10-01 00:00:00+02 | 21
6 | 2015-10-02 00:00:00+02 | 657
7 | 2015-10-01 00:00:00+02 | 43
7 | 2015-10-02 00:00:00+02 | 432
8 | 2015-10-01 00:00:00+02 | 18
8 | 2015-10-02 00:00:00+02 | 421
9 | 2015-10-01 00:00:00+02 | 14
9 | 2015-10-02 00:00:00+02 | 432
10 | 2015-10-01 00:00:00+02 | 23
10 | 2015-10-02 00:00:00+02 | 454
11 | 2015-10-01 00:00:00+02 | 54
11 | 2015-10-02 00:00:00+02 | 654
(12 rows)
C REATE EXTENSION:
CREATE EXTENSION tablefunc;
クロス集計クエリ
SELECT * FROM crosstab(
'SELECT zoom, day, point
FROM pivot
ORDER BY 1,2'
,$$VALUES ('2015-10-01'::timestamp), ('2015-10-02')$$)
AS ct ("zoom" smallint, "2015-10-01" integer, "2015-10-02" integer);
結果:私はポイントの値を返すことはできません
zoom | 2015-10-01 | 2015-10-02
------+------------+------------
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
(6 rows)
、クエリ自体は私の空のスポットを提供します。私は間違って何をしていますか?
編集
私はそれを他の方法を実行しようとしましたが、私はまだ上記の質問に対する答えを探しています。
SELECT * from crosstab (
'select zoom, day, point
from pivot
order by 1,2',
'select distinct day from pivot order by 1')
AS ct(zoom smallint, "2015-10-01" integer, "2015-10-02" integer);
結果:@Abelisto提案へ
zoom | 2015-10-01 | 2015-10-02
------+------------+------------
6 | 21 | 657
7 | 43 | 432
8 | 18 | 421
9 | 14 | 432
10 | 23 | 454
11 | 54 | 654
(6 rows)
は時間zone'とし、一日のタイムスタンプ 'テーブル内のタイプのマッチングを行います'' 2015-10-01 ':: timestamp'(間違っていて、 ''2015-10-01' :: timestamptz'でなければなりません) – Abelisto