Postgres: select the sum of values and then sum this again

I would use the ROLLUP function on POSTRGESQL:

SELECT name,sum(size) as total
FROM mytable 
group by ROLLUP(name )
order by name;

This will give you a grand total of any value that you aggregate and can also be used for aggregating multiple columns.

Hope it helps!


Try this:

SELECT sum(a.total)
FROM (SELECT sum(size) as total
      FROM mytable group by name) a

UPDATE I'm sorry, I don't read that you want it all in the same query. For this reason the answer of greg it's better. However, other possibility if you have a postgresql version >= 9:

WITH mytableWith (name, sum) as
     (SELECT name, sum(size)
      FROM mytable
      GROUP BY name)
SELECT 'grand total' AS name, 
       sum(sum) AS sum
FROM mytableWith
UNION ALL
SELECT name, sum
FROM mytableWith

If you want all results with the same SELECT, you could do something like

SELECT 
  'grand total' AS name, 
   sum(size) AS sum 
FROM 
  mytable 
UNION ALL 
SELECT 
  name, 
  sum(size) AS sum 
FROM 
  mytable 
GROUP BY 
  name;

Hope it helps…