PostGIS: how to retrieve a report of costs by decades from a table I have a table with a list of 'films' and, for each film, information about the 'company' that produced, the 'cost' of the production and the 'year' of production. I want to consult the 'total_cost' that each 'company' spent every 'decade' from the 1940s to the 1990s, so the information retrieved should be a table with the following columns:   


'company'
'sum_cost_1940s'
'sum_cost_1950s'
'sum_cost_1960s'
'sum_cost_1970s'
'sum_cost_1980s'
'sum_cost_1990s'
'total_sum'


I´ve been able to obtain a new table all the information in three columns ('company', 'decade', 'sum_total_decade'), with the next consult:   

SELECT company, sum(cost) AS total_cost_by_decade, trunc(year,-1) AS decada   FROM data_table GROUP BY company, decada ORDER BY company, decada ASC  


And I've benn able to obtain a new table with the columns 'company', 'sum_cost_1950s', 'total_sum', using the next consultation:

SELECT base.company AS company, 
       decada5.sum_cost AS sum_cost_1950s, 
       base.total_sum 
FROM   (SELECT company, 
               sum(cost) AS total_sum 
       FROM data_table 
       GROUP BY company 
       ORDER BY company ASC) base,
       (SELECT company, 
               sum(cost) AS sum_cost 
       FROM data_table 
       WHERE trunc(ano,-1) = '1950' 
       GROUP BY company ORDER BY company ASC) decada5 
WHERE base.company = decada5.company


But I wans't able to increase the table, with only one SELECT, with the other decades. How can I to it?
Postgres allow to extract DECADE from a timestamp value, use a CASE statment to sum each one.

SELECT 
    company, 
    sum(case when extract(DECADE from movie_date) = 195 then cost else 0 end) as Total_Decade_50s,
    sum(case when extract(DECADE from movie_date) = 196 then cost else 0 end) as Total_Decade_60s,
    sum(case when extract(DECADE from movie_date) = 197 then cost else 0 end) as Total_Decade_70s,
    sum(case when extract(DECADE from movie_date) = 198 then cost else 0 end) as Total_Decade_80s,
    sum(case when extract(DECADE from movie_date) = 199 then cost else 0 end) as Total_Decade_90s,
    sum(case when extract(DECADE from movie_date) = 200 then cost else 0 end) as Total_Decade_00s,
FROM 
    data_table 
GROUP BY 
    company
ORDER BY 
    company;


In case you get an error on extract sentence, cast your date as timestamp.

extract(DECADE from film_date::timestamp)