Sql 如何在postgres中选择列?
我的问题是:Sql 如何在postgres中选择列?,sql,postgresql,Sql,Postgresql,我的问题是: 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; Pos
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返回我以下错误:
错误:没有列“所有\u 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'
);
这就消除了聚合。首先,学习使用正确的连接
语法
其次,您可以使用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'
);
这消除了聚合