2014-11-02 55 views
0

問題設置單獨的行和扁平列信息

我有一個表

id type x y 
1  type1 1.0 2.0 
2  type2 1.2 2.3 
3  type1 1.2 2.4 

我要像下面分離X,Y Type1和Type2的:

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中實現這一點?

回答

1

我只想用條件彙總:

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;