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

后端 未结 4 1649
太阳男子
太阳男子 2020-12-24 09:14

I tried a lot but can´t find the right way. If I select values in Postgres and sum them it looks like this:

SELECT name,sum(size) as total
FROM mytable group         


        
4条回答
  •  生来不讨喜
    2020-12-24 09:42

    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…

提交回复
热议问题