Group by alias (Oracle)

后端 未结 5 1478
独厮守ぢ
独厮守ぢ 2020-11-28 08:07

How to \'group by\' a query using an alias, for example:

select count(*), (select * from....) as alias_column 
from table 
group by alias_column
5条回答
  •  -上瘾入骨i
    2020-11-28 08:42

    To use an alias in Oracle you need to ensure that the alias has been defined by your query at the point at which the alias is being used.

    The most straightforward way to do this is to simply treat the original query as a subquery -- in this case,

    select count(*), (select * from....) as alias_column 
    from table 
    group by (select * from....)
    

    becomes

    select count, alias_column 
    from
      (select count(*) as count, (select * from....) as alias_column 
      from table)
    group by alias_column 
    

    I can't speak to the performance implications, but it's very quick to write if you're trying to re-use an alias in your query - throw everything in parentheses and jump up a level...

提交回复
热议问题