重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 637|回复: 0
打印 上一主题 下一主题

ORACLE 23C新特性之二:group by +字段别名

[复制链接]
跳转到指定楼层
楼主
发表于 2023-4-11 22:42:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在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.

看来确实要方便一些。




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-4-27 10:08 , Processed in 0.109268 second(s), 19 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表