Sql 按组分隔行并展平列信息

Sql 按组分隔行并展平列信息,sql,postgresql,pivot,Sql,Postgresql,Pivot,问题设置 我有一张桌子 id type x y 1 type1 1.0 2.0 2 type2 1.2 2.3 3 type1 1.2 2.4 我想将类型1和类型2的x,y分开,如下所示: id x_type1 y_type1 x_type2 y_type2 1 1.0 2.0 2 1.2 2.3 3 1.2

问题设置

我有一张桌子

id    type    x    y
1     type1   1.0  2.0
2     type2   1.2  2.3
3     type1   1.2  2.4
我想将类型1和类型2的x,y分开,如下所示:

id    x_type1   y_type1  x_type2    y_type2
1     1.0       2.0       
2                        1.2        2.3
3     1.2       2.4

如何在postgresql中实现这一点

我只会使用条件聚合:

select t.id,
       max(case when type = 'type1' then x end) as x_type1,
       max(case when type = 'type1' then y end) as y_type1,
       max(case when type = 'type2' then x end) as x_type2,
       max(case when type = 'type3' then y end) as y_type2
from table t
group by t.id;