I have a view that produces the following resultset:
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);
And I need to transform it into this:
client_id a0_cur_val a0_fut_val a1_cur_val a1_fut_val ...
1 10 20 5 10
2 NULL NULL 0 2
I know how to do this if I use just the current_value
column, using crosstab. How can I use current_value
and future_value
to produce new columns in the destination resultset? If I just add future_value
column to the crosstab(text)
query it complains about "invalid source data SQL statement".
I'm using PostgreSQL 9.3.6.