1

CREER TEMPORAIRE TABLEComment créer un tableau croisé dans PostgreSQL?

CREATE TEMP TABLE pivot(
gid SERIAL, 
zoom smallint NOT NULL, 
day timestamp with time zone NOT NULL, 
point integer NOT NULL 
); 

Mentionner les données

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); 

permet de voir si tout fonctionne jusqu'à présent:

SELECT zoom, day, point 
FROM pivot 
ORDER BY 1,2; 

Résultat:

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 EXTENSION RÉER:

CREATE EXTENSION tablefunc; 

CROSSTAB QUERY

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); 

Résultat:

zoom | 2015-10-01 | 2015-10-02 
------+------------+------------ 
    6 |   | 
    7 |   | 
    8 |   | 
    9 |   | 
    10 |   | 
    11 |   | 
(6 rows) 

Je ne peux pas retourner les valeurs des points, la requête elle-même me donne un taches vides. Qu'est-ce que je fais mal?

Modifier

J'ai essayé de le faire dans l'autre sens, mais je suis toujours à la recherche de la réponse à la question ci-dessus.

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); 

Résultat:

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) 
+2

Faire le type correspondant dans le tableau 'horodatage de jour avec le temps zone' et dans le query ''2015-10-01' :: timestamp' (faux, devrait être' '2015-10-01' :: timestamptz') – Abelisto

Répondre

2

Merci à la suggestion @Abelisto à propos timestamptz, cela fonctionne:

SELECT * FROM crosstab(
     'SELECT zoom, day, point 
     FROM pivot 
     ORDER BY 1,2' 
     ,$$VALUES ('2015-10-01'::timestamptz), ('2015-10-02')$$) 
AS ct ("zoom" smallint, "2015-10-01" integer, "2015-10-02" integer);