我有一个产生以下结果集视图:Postgres的 - 与多个值列数据透视表/交叉
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。
我如果有人认为它可以帮助只用一个柱后的交叉表查询。 –
发布您迄今为止/尝试的内容总是有帮助的。 –