gpt4 book ai didi

sql - 如何在postgres中选择一列?

转载 作者:行者123 更新时间:2023-11-29 14:10:33 25 4
gpt4 key购买 nike

我的查询:

select c.id, sum(case when r.paid_out>='1' then 0 else 1 end) as all_paids
from people p1, houses h, policies p2, receipts r
where p1.id = h.id
and h.code = p2.code
and p2.code_policy = r.code_policy
group by p1.id
having all_paids= 0;

Postesql 返回以下错误:

错误:没有“all_paids”列

我试过很多东西,但没有任何帮助,感谢您的帮助

最佳答案

首先,学习使用正确的 join 语法。

其次,您可以为 having 子句或子查询使用表达式:

select p1.id
from people p1 join
houses h
on p1.id = h.id join
policies p2
on h.code = p2.code join
receipts r
on p2.code_policy = r.code_policy
group by p1.id
having sum(case when r.paid_out >= '1' then 0 else 1 end) = 0;

注意:没有定义c.id,所以我假设select应该是p1.id基于分组依据

我应该说这个逻辑通常会用not exists来表达:

select p1.*
from people p1
where not exists (select 1
from houses h
policies p2
on h.code = p2.code join
receipts r
on p2.code_policy = r.code_policy
where p1.id = h.id and
r.paid_out >= '1'
);

这消除了聚合。

关于sql - 如何在postgres中选择一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37100552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com