Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在postgres中选择列?_Sql_Postgresql - Fatal编程技术网

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'
                 );
这消除了聚合