我有一个视图,它生成了以下结果集:
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
和future_value
来生成目标结果集中的新列,该怎么办呢?如果我只是将crosstab(text)
查询中的future_value
列添加进去,它会报错,提示“无效的源数据SQL语句”。我正在使用PostgreSQL 9.3.6版本。