This article discusses how to use dynamic SQL technology to convert tabular data into pivot table format, focusing on improving the efficiency and flexibility of data conversion.
The following query uses the CASE statement and GROUP BY to implement data pivoting:
SELECT bar, MAX(CASE WHEN abc."row" = 1 THEN feh ELSE NULL END) AS "val1", MAX(CASE WHEN abc."row" = 2 THEN feh ELSE NULL END) AS "val2", MAX(CASE WHEN abc."row" = 3 THEN feh ELSE NULL END) AS "val3" FROM ( SELECT bar, feh, row_number() OVER (partition by bar) as row FROM "Foo" ) abc GROUP BY bar
This method is effective for pivoting data, but is less efficient and less flexible when dealing with large data sets.
function provided by the tablefunc
module of crosstab()
PostgreSQL can provide a more efficient and dynamic solution.
Install tablefunc module
Before using crosstab()
, you need to ensure that the tablefunc
module has been installed into the PostgreSQL database. You only need to execute the following command once per database:
CREATE EXTENSION tablefunc;
Crosstab implementation
The following crosstab
queries can be used for pivoting:
SELECT * FROM crosstab( 'SELECT bar, 1 AS cat, feh FROM tbl_org ORDER BY bar, feh') AS ct (bar text, val1 int, val2 int, val3 int); -- 可根据需要增加列
Instructions:
tbl_org
is the input table. crosstab()
selects bar
, a dummy category cat
, and feh
. The cat
column only serves as a placeholder and will be ignored. ORDER BY
clause ensures that the values are in proper order. This query will return data in the desired pivot table format.
In order to achieve more dynamic applications, we can use window functions to synthesize category columns:
SELECT * FROM crosstab( $$ SELECT bar, val, feh FROM ( SELECT *, 'val' || row_number() OVER (PARTITION BY bar ORDER BY feh) AS val FROM tbl_org ) x ORDER BY 1, 2 $$ , $$VALUES ('val1'), ('val2'), ('val3')$$ -- 可根据需要增加列 ) AS ct (bar text, val1 int, val2 int, val3 int); -- 可根据需要增加列
This query dynamically creates category columns based on values in the table.
All in all, the crosstab()
function provides a more efficient and flexible solution for pivoting in SQL, simplifying queries and allowing flexible customization according to needs.
The above is the detailed content of How Can I Efficiently Pivot Data in SQL Using Dynamic SQL Techniques?. For more information, please follow other related articles on the PHP Chinese website!