Postgres Error: missing FROM-clause entry for table

Postgres Error: missing FROM-clause entry for table

我有一个查询,正在使用左联接。我的左连接子句如下:

left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
                    from partner_user_profiles pup
                    join partner_user_roles pure on pure.user_profile_id = pup.id
                    join profile_coverage_zips pcz on pcz.profile_id = pup.id
                    where lower(pure.role) = 'agent'
                    and pup.verification_status like 'Verified%'
                    group by pup.brokerage_code, pcz.zip) vac on vac.brokerage_code = b.brokerage_code and pcz.zip = bcz.zip

但是我收到错误消息说我缺少“pcz”的 FROM 条目子句但是我在连接子句中为 table 添加了别名所以我不确定哪里出了问题。

您已在 sub-select 中定义了 table 别名 pcz,但是在 sub-select 外部时别名不再存在。在您使用它时,适当的别名是整个 sub-select 的别名,在本例中是 vac。所以:vac.zip = = bcz.zip

left outer join ( select pup.brokerage_code, pcz.zip, count (pup.aggregate_id) as VerifiedAgentCount
                    from partner_user_profiles pup
                    join partner_user_roles pure on pure.user_profile_id = pup.id
                    join profile_coverage_zips pcz on pcz.profile_id = pup.id
                    where lower(pure.role) = 'agent'
                    and pup.verification_status like 'Verified%'
                    group by pup.brokerage_code, pcz.zip
                ) vac on vac.brokerage_code = b.brokerage_code 
                     and vac.zip = bcz.zip