我有一个查询,我试图在其中添加一个列,该列包含具有特定值的记录相对于记录总数的百分比。我可以让子查询自行工作,但是当我将其插入主查询时,它会失败并出现错误:
ERROR: subquery uses ungrouped column "ipam_addresses.host_id" from outer query
我的查询是:
select
ipam_networks.ipam_id,
ipam_networks.net_name,
ipam_networks.net_cidr,
zone,
count(*),
to_char(100*(select count(*) where ipam_addresses.host_id = 0) / (select count(*)), '999D99%') v
from
ipam_addresses
left outer join
ipam_networks
on ipam_addresses.parent_id = ipam_networks.ipam_id
where
ipam_addresses.ip_state != 'DHCP_FREE'
group by
ipam_networks.ipam_id, ipam_networks.net_name, ipam_networks.net_cidr;
为什么即使我在子查询中指定了表名,它也会抛出这个错误,我能做些什么来解决这个问题?
您可以使用过滤的聚合来做到这一点,根本不需要子选择
需要强制转换
::numeric
以避免整数除法