Invalid identifier error on field created with select statement

孤街醉人 提交于 2020-01-15 11:11:24

问题


Why sql bellow don't work?

select
    a.field1, a.field2, a.field3,
    (select count(*)
        from table2 b
        where b.field1 = a.field1
    ) as field4,
    (select count(*)
        from table3 b
        where b.field1 = a.field1
    ) as field5,
    (select count(*)
        from table4 b
        where b.field1 = a.field1
    ) as field6,
from table1 a
order by field4

Oracle says: ORA-00904: "field4": invalid identifier


回答1:


try to wrap it up

select * from 
(    
select
    a.field1, a.field2, a.field3,
    (select count(*)
        from table2 b
        where b.field1 = a.field1
    ) as field4,
    (select count(*)
        from table3 b
        where b.field1 = a.field1
    ) as field5,
    (select count(*)
        from table4 b
        where b.field1 = a.field1
    ) as field6,
from table1 a
)
order by field4


来源:https://stackoverflow.com/questions/7732019/invalid-identifier-error-on-field-created-with-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!