如何使用别名"分组"查询,例如:
select count(*), (select * from....) as alias_column from table group by alias_column
我得到'alias_column':INVALID_IDENTIFIER错误消息.为什么?如何分组此查询?
select count(count_col), alias_column from ( select count_col, (select value from....) as alias_column from table ) as inline group by alias_column
如果在GROUP BY子句中重复相应的表达式,则分组通常有效.只提一个别名是不可能的,因为SELECT步骤是执行查询的最后一步,分组发生在早期,当别名尚未定义时.
对于GROUP BY子查询的结果,您将需要稍微绕道并使用嵌套查询,如上所示.
使用别名列嵌套查询:
select count(*), alias_column from ( select empno, (select deptno from emp where emp.empno = e.empno) as alias_column from emp e ) group by alias_column;