2015-11-03 76 views
0

計數空/通用行,我想產生這樣的使用Postgres的,從一些任意的標準:基於從不同的表

原始表(T1):

type | count 
------------ 
    1 | 2 
    2 | 3 
    3 | 1 

生成的表:

type_1 | type_2 | type_3 
------------------------ 
true | null | null 
true | null | null 
null | true | null 
null | true | null 
null | true | null 
null | null | true 

回答

0

以下基本完成你想要什麼:

with x as (
     select 1 as type, 2 as cnt union all 
     select 2, 3 union all 
     select 3, 1 
    ) 
select x.type = 1, x.type = 2, x.type = 3 
from x, lateral 
    generate_series(1, x.cnt); 

這產生false而不是NULL,但更復雜的case表達式可以很容易地解決這個問題。