有类似的问题,但对我没有用。
我需要获得一个唯一的表作为选择,在这里我可以找到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
答案 0 :(得分:0)
您可以嵌套日期并计算括号外的百分比。
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