在同一百分比查询中使用两个 SUM 计算的结果

Using results of two SUM calculation in same query for a percentage

有类似的问题,但对我没有用。

我需要获得一个唯一的 table 作为选择,我可以在其中找到 3 个计算字段,两个 SUM,然后我需要计算它们的百分比。我该怎么做?

SELECT
pro_com,
(SUM(presence) FILTER(WHERE date_block = '2016-10-09 03:00:00')::integer AS date1,
(SUM(presence) FILTER(WHERE date_block = '2016-10-16 03:00:00')::integer AS date2,
(date1/date2*100)::integer AS percentage
FROM tab_presence
GROUP BY pro_com 

您可以嵌套日期并计算括号外的百分比。

SELECT 
       pro_com, 
       date1, 
       date2, 
       ( date1 / date2 * 100 )::INTEGER AS percentage 
FROM   
 ( 
SELECT   
  pro_com, 
   SUM(presence) FILTER(WHERE date_block = '2016-10-09 03:00:00')::INTEGER AS date1, 
   SUM(presence) FILTER(WHERE date_block = '2016-10-16 03:00:00')::INTEGER AS date2     
  FROM  tab_presence 
 GROUP BY pro_com 
   ) s