PostgreSQL计算3个连接的百分比

时间:2019-03-02 20:35:03

标签: postgresql join

我正在计算百分比值以指示进度。我将来自同一列(但值不同)的3个独立计数的数据连接起来。我距离很近,但是一直给我零。

SELECT a.count1 / (a.count1 + b.count2 + c.count3) FROM
(SELECT count(*) AS "count1"
FROM "stitch_jira"."issues"
WHERE "stitch_jira"."issues"."fields__status__statusCategory__name" = 'To Do') a
CROSS JOIN 
(SELECT count(*) AS "count2"
FROM "stitch_jira"."issues"
WHERE ("stitch_jira"."issues"."fields__status__statusCategory__name" = 'In Progress')) b  
CROSS JOIN 
(SELECT count(*) AS "count3"
FROM "stitch_jira"."issues"
WHERE ("stitch_jira"."issues"."fields__status__statusCategory__name" = 'Done')) c

关于为什么我在这里得到零结果的任何建议?它应该产生“ 0.4 ...”

1 个答案:

答案 0 :(得分:1)

您可以使用条件聚合重写整个查询,并强制转换为十进制/乘以1.0:

SELECT 
 1.0*(COUNT(*) FILTER(WHERE fields__status__statusCategory__name='To Do'))/
 (COUNT(*) FILTER(WHERE fields__status__statusCategory__name 
                   IN('To Do','In Progress','Done')))
FROM "stitch_jira"."issues"