在23c之前,相信大家,尤其是初学者,经常遇到在GROUP BY 后面不能跟字段别名的情况,如果是一个比较复杂的表达式,在GROUP BY 后面输入,不太方便,很多时候,认为ORDER BY 后面都可以跟字段别名,或字段顺序号,那GROUP BY 后面也可以,其实不然,见下面的19C例子:
SQL> select banner from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
SQL> select department_id deptid ,sum(salary) from hr.employees group by deptid;
select department_id deptid ,sum(salary) from hr.employees group by deptid
*
ERROR at line 1:
ORA-00904: "DEPTID": invalid identifier
SQL>
这里报错了,在GROUP BY 后面需要实际的字段名,这个问题,在23C解决了,可以直接在group BY 后面直接跟字段别名或顺序号,如下:
SQL> select BANNER from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 23c Free, Release 23.0.0.0.0 - Developer-Release
SQL> select department_id deptid,sum(salary)
2 from employees
3 group by deptid;
DEPTID SUM(SALARY)
---------- -----------
90 58000
60 19200
50 17500
80 30100
7000
10 4400
20 19000
110 20308
8 rows selected.
having 后面也可以使用别名:
SQL> select department_id deptid,sum(salary) as totalsal
2 from employees
3 group by deptid
4 having totalsal >10000;
DEPTID TOTALSAL
---------- ----------
90 58000
60 19200
50 17500
80 30100
20 19000
110 20308
6 rows selected.
看来确实要方便一些。
|