首页 > 解决方案 > 为什么我在输入结束时收到错误:语法错误?

问题描述

我不断收到“输入结束时的语法错误”,不知道为什么。

我想要做的是将疾病结果除以总结果,并在疾病部分显示 condition_id。

select disease.condition_id, (disease::float/total::float) as prevalence
from (
    select condition_id, count(person_id)
    from a.condition
    where condition_id=316139
    group by condition_id
    ) as disease
join (
    select count(distinct person_id) as total
    from a.person
    )as total;

有人可以帮我吗?

谢谢!

标签: postgresql

解决方案


对于您当前的语法,我没有确切的修复方法,但我会将这个查询表述为对整个表进行聚合的连接:

SELECT
    COUNT(*) FILTER (WHERE c.condition_id = 316139) /
        COUNT(DISTINCT p.person_id) AS prevalence
FROM a.person p
LEFT JOIN a.condition c
    ON p.person_id = c.person_id;

推荐阅读