【发布时间】:2015-07-13 21:54:16
【问题描述】:
我有一个生成以下结果集的视图:
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 在目标结果集中生成新列?如果我只是将future_value 列添加到crosstab(text) 查询中,它会抱怨“无效的源数据SQL 语句”。
我使用的是 PostgreSQL 9.3.6。
【问题讨论】:
-
如果有人认为有帮助,我可以只使用一列发布交叉表查询。
-
发布到目前为止您已经/尝试过的内容总是有帮助的。
标签: sql postgresql crosstab