我有一个产生以下结果集的视图:
CREATE TABLE foo
AS
SELECT client_id, asset_type, current_value, future_value
FROM ( VALUES
( 1, 0, 10 , 20 ),
( 1, 1, 5 , 10 ),
( 1, 2, 7 , 15 ),
( 2, 1, 0 , 2 ),
( 2, 2, 150, 300 )
) AS t(client_id, asset_type, current_value, future_value);
我需要把它改成这样:
client_id a0_cur_val a0_fut_val a1_cur_val a1_fut_val ...
1 10 20 5 10
2 NULL NULL 0 2
如果我只使用current_value
列,使用交叉表。我该如何使用current_value
and future_value
在目标结果集中生成新列?如果我只是添加future_value
列到crosstab(text)
查询时它抱怨“源数据 SQL 语句无效”。
我正在使用 PostgreSQL 9.3.6。