Home >Database >Mysql Tutorial >How Can I Dynamically Transform Row-Oriented Data into Column-Oriented Data in PostgreSQL?
CASE
and GROUP BY
vs. crosstab
function In PostgreSQL, many users need to convert row data tables into column data tables for analysis and reporting. While CASE
and GROUP BY
are commonly used methods, more dynamic and efficient alternatives exist.
For example, consider the following table:
id | feh | bar |
---|---|---|
1 | 10 | A |
2 | 20 | A |
3 | 3 | B |
4 | 4 | B |
5 | 5 | C |
6 | 6 | D |
7 | 7 | D |
8 | 8 | D |
Desired Output Convert this data to the following format:
bar | val1 | val2 | val3 |
---|---|---|---|
A | 10 | 20 | NULL |
B | 3 | 4 | NULL |
C | 5 | NULL | NULL |
D | 6 | 7 | 8 |
CASE
and GROUP BY
One way to achieve this is to use the CASE
and GROUP BY
techniques:
<code class="language-sql">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 </code>
While this approach works, it has potential limitations in terms of flexibility and performance.
crosstab
Functions: a dynamic solution function introduced by the tablefunc
module in PostgreSQL is a more dynamic and efficient alternative. Using the crosstab()
function, you can achieve the desired transformation without explicitly defining the output columns. crosstab()
crosstab
solution is as follows: crosstab
<code class="language-sql">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);</code>In this query,
provides data in a format compatible with SELECT bar, 1 AS cat, feh
. The crosstab()
column is used as a dummy placeholder since there is no category information in the table. cat
crosstab
<code class="language-sql">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);</code>News
crosstab
module. tablefunc
function provides a powerful and dynamic alternative to the crosstab()
and CASE
methods. It allows flexible data transformation, making it a valuable tool for data manipulation in PostgreSQL. GROUP BY
The above is the detailed content of How Can I Dynamically Transform Row-Oriented Data into Column-Oriented Data in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!