Herramientas de usuario

Herramientas del sitio


informatica:base_de_datos:postgresql:postgresql_pivot

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
informatica:base_de_datos:postgresql:postgresql_pivot [2026/01/28 04:11] admininformatica:base_de_datos:postgresql:postgresql_pivot [2026/01/29 02:50] (actual) admin
Línea 6: Línea 6:
   * [[https://www.beekeeperstudio.io/blog/how-to-pivot-in-postgresql/]]   * [[https://www.beekeeperstudio.io/blog/how-to-pivot-in-postgresql/]]
     * [[https://rextester.com/DMRIM25761]]     * [[https://rextester.com/DMRIM25761]]
-  * [[https://learnsql.com/blog/creating-pivot-tables-in-postgresql-using-the-crosstab-function/]]+  * [[https://stackoverflow.com/questions/3002499/postgresql-crosstab-query]] 
 + 
 +<code postgresql
 + 
 +CREATE EXTENSION IF NOT EXISTS tablefunc; 
 + 
 +CREATE TABLE tbl ( 
 +   section   text 
 + , status    text 
 + , ct        integer  -- "count" is a reserved word in standard SQL 
 +); 
 + 
 +INSERT INTO tbl VALUES  
 +  ('A', 'Active', 1),  
 +  ('A', 'Inactive', 2), 
 +  ('B', 'Active', 4), 
 +  ('B', 'Inactive', 5), 
 +  ('C', 'Inactive', 7);  -- ('C', 'Active') is missing 
 + 
 + 
 +SELECT * 
 +FROM   crosstab
 +   'SELECT section, status, ct 
 +    FROM   tbl 
 +    ORDER  BY 1,2'  -- needs to be "ORDER BY 1,2" here 
 +   ) AS ct ("Section" text, "Active" int, "Inactive" int); 
 + 
 +</code>
  
  
  
  
informatica/base_de_datos/postgresql/postgresql_pivot.1769573490.txt.gz · Última modificación: 2026/01/28 04:11 por admin