Home > database >  Postgres field more than a line into a matrix structure
Postgres field more than a line into a matrix structure

Time:10-11

I have a table has the following fields:
Id, a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3 and c4 (actual project more than dozens of fields, and in the N record of id primary key)
Want to use SQL or PGPLSQL into structured data as follows:
Id, a1, b1, c1
Id, a2, b2, c2
Id, a3, b3, c3
Id, a4, b4 and c4

Please directly, thank you!

CodePudding user response:

The array_agg () function in the output array, it is you need?

CodePudding user response:

First think about what you want, this is a clear need to row data into N rows, so it must be with
 
Select id, a1, b1, c1 from xx
Union all
Select id, a2, b2, c2 from xx
.


Or use conversion, tablefunc extension to understand, but for ordinary programmers obviously the first solution more quickly

CodePudding user response:

Can use tablefunc crosstab function module, refer to: https://www.postgresql.org/docs/12/tablefunc.html
  • Related