在postgresql中计算百分比的问题

时间:2017-07-04 05:06:18

标签: postgresql

这是我的查询。每个查询(计数器,分母)单独工作。当我试图将这两个查询分开时,我得到了这个  错误:“/”处或附近的语法错误 我想在2016年的前3个月(SPORT& MUSIC)从同一部门的2016年总价中获得总价的百分比

(SELECT "Total Price"
FROM "Sales"
WHERE "Department" IN('SPORT','MUSIC')
AND "DATE" BETWEEN '2016-01-01' AND '2016-03-31')/(SELECT "Total Price"
FROM "Sales"
WHERE "Department" IN('SPORT','MUSIC')
AND "DATE" BETWEEN '2016-01-01' AND '2016-12-31') AS percentage

1 个答案:

答案 0 :(得分:0)

我猜你的意思是

SELECT
   "Total Price"/(sum("Total Price") OVER ()) * 100.0 AS percentage
FROM "Sales"
WHERE "Department" IN ('SPORT','MUSIC')
  AND "DATE" BETWEEN '2016-01-01' AND '2016-03-31';

请帮忙,避免使用大写或混合大小写和非字母数字字符的名称。