This is a general response to the collapse equivalent in Postgresql.
Given table t:
create table t (l1 char(1), l2 char(1), i integer); insert into t (l1, l2, i) values ('A', 'X', 1), ('A', 'Y', 2), ('B', 'X', 3), ('B', 'Y', 4);
And this SQL Server query: SQL Fiddle
select l1, l2, sum(i) total from t group by rollup(l1, l2)
Here's how to do it in Postgresql: SQL Fiddle
Create aggregate CTEs, starting with drillthroughs going to the top level:
with detail as ( select l1, l2, sum(i) total from t group by l1, l2 ), l2 as ( select l1, sum(total) total from detail group by l1 ), l1 as ( select sum(total) total from l2 )
Note that for performance, the next level up is aggregated from the previous level down.
With this, you can simply combine the CTE by creating the appropriate labels:
select l1, l2, total from detail union select l1, 'Total', total from l2 union select 'Total', '', total from l1 order by l1, l2