如何解决PostgreSQL:错误:列“ bb”不存在
我不擅长SQL,我想在下面的查询中使用partition by by MAX,但是当我使用相同的查询但没有该max drive列的where子句时,它表示该列不存在,但是如果我删除了我可以从中选择相同的列。
select
MAX(case when total_split_count = 0 or total_split_count is null then total_split_count else 1 end) OVER (PARTITION BY ia.col1,ia.col2,ia.col3,ia.col4,ia.col5,ia.col6) as bb
from audits.tbl_name ia
where bb = 1
错误:列“ bb”不存在位置:304
其中bb = 1 ^ 1条语句失败。
但查询使用where子句运行:
select
MAX(case when total_split_count = 0 or total_split_count is null then total_split_count else 1 end) OVER (PARTITION BY ia.col1,ia.col6) as bb
from audits.tbl_name ia
注意:我在运行时通过“ as”创建了该列。
解决方法
在select
子句中不可见的where
子句中定义的别名。
使用
select * from (select ... as bb from audits.tbl_name ia) x where bb = 1
或CTE:
with x as (select ... as bb from audits.tbl_name ia) select * from x where bb = 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。