PostgreSQL - Consulta sobre Referencias cruzadas

 
Vista:
Imágen de perfil de John
Val: 14
Ha mantenido su posición en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Consulta sobre Referencias cruzadas

Publicado por John (6 intervenciones) el 14/10/2019 17:36:33
Hola amigos, tengo el siguiente código que me funciona hasta el momento perfecto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
CREATE TABLE sales(
   id serial,
   country VARCHAR(3),
   type VARCHAR(15),
   month int,
   amount numeric(10,2)
);
 
INSERT INTO sales(country,type,month,amount)
VALUES ('PEN','Course',1,16),('PEN','Course',2,12),('PEN','Course',3,24),
       ('PEN','Subscription',4,30),('PEN','Subscription',5,30),('PEN','Subscription',6,30),
       ('PEN','Course',7,16),('PEN','Subscription',8,30),('PEN','Course',9,16),
       ('PEN','Subscription',10,30),('PEN','Subscription',11,30),('PEN','Course',12,12),
       ('COL','Subscription',1,30),('COL','Course',4,24),('COL','Subscription',6,30),
       ('COL','Subscription',12,30),('BOL','Course',1,12),('BOL','Course',3,12),
       ('MXN','Course',5,16);
 
CREATE EXTENSION IF NOT EXISTS tablefunc;
 
SELECT split_part(key,'-',1) AS country, split_part(key,'-',2) AS type, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
    FROM crosstab(
      'SELECT CONCAT(country,''-'',type) AS key, month, SUM(amount) FROM sales GROUP BY key, month ORDER BY 1,2',
      'SELECT month FROM generate_series(1,12) AS month'
) AS (
    key text,
    Jan NUMERIC(10,2),
    Feb NUMERIC(10,2),
    Mar NUMERIC(10,2),
    Apr NUMERIC(10,2),
    May NUMERIC(10,2),
    Jun NUMERIC(10,2),
    Jul NUMERIC(10,2),
    Aug NUMERIC(10,2),
    Sep NUMERIC(10,2),
    Oct NUMERIC(10,2),
    Nov NUMERIC(10,2),
    Dec NUMERIC(10,2)
);
dandome como resultado algo así:

total

la pregunta es: ¿Se puede generar una fila extra llamada por ejemplo total, que me sume el total de cada columna?, así como también lo mismo un total para cada fila?


total2
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Francisco
Val: 256
Oro
Ha mantenido su posición en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Consulta sobre Referencias cruzadas

Publicado por Francisco (110 intervenciones) el 14/10/2019 17:56:59
Hola

Te ayudas con un CTE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
WITH t AS (
    SELECT split_part(key,'-',1) AS country, split_part(key,'-',2) AS type, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
        FROM crosstab(
          'SELECT CONCAT(country,''-'',type) AS key, month, SUM(amount) FROM sales GROUP BY key, month ORDER BY 1,2',
          'SELECT month FROM generate_series(1,12) AS month'
    ) AS (
        key text,
        Jan NUMERIC(10,2),
        Feb NUMERIC(10,2),
        Mar NUMERIC(10,2),
        Apr NUMERIC(10,2),
        May NUMERIC(10,2),
        Jun NUMERIC(10,2),
        Jul NUMERIC(10,2),
        Aug NUMERIC(10,2),
        Sep NUMERIC(10,2),
        Oct NUMERIC(10,2),
        Nov NUMERIC(10,2),
        Dec NUMERIC(10,2)
    )
)
SELECT
    *,
    COALESCE(jan,0)+
    COALESCE(feb,0)+
    COALESCE(mar,0)+
    COALESCE(apr,0)+
    COALESCE(may,0)+
    COALESCE(jun,0)+
    COALESCE(jul,0)+
    COALESCE(aug,0)+
    COALESCE(sep,0)+
    COALESCE(oct,0)+
    COALESCE(nov,0)+
    COALESCE(dec,0) total_country_type
FROM T
UNION ALL
SELECT '' Country, 'total' "type",
SUM(jan),
SUM(feb),
SUM(mar),
SUM(apr),
SUM(may),
SUM(jun),
SUM(jul),
SUM(aug),
SUM(sep),
SUM(oct),
SUM(nov),
SUM(dec),
SUM(
    COALESCE(jan,0)+
    COALESCE(feb,0)+
    COALESCE(mar,0)+
    COALESCE(apr,0)+
    COALESCE(may,0)+
    COALESCE(jun,0)+
    COALESCE(jul,0)+
    COALESCE(aug,0)+
    COALESCE(sep,0)+
    COALESCE(oct,0)+
    COALESCE(nov,0)+
    COALESCE(dec,0)
) total_country
FROM t

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar
Imágen de perfil de John
Val: 14
Ha mantenido su posición en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Consulta sobre Referencias cruzadas

Publicado por John (6 intervenciones) el 14/10/2019 18:49:04
Justo lo que necesitaba, muchas gracias Francisco
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar