SQL KEEP 窗口函数等价改写案例

一哥们出条sql题给我玩,将下面sql改成不使用keep分析函数的写法。

复制代码
select deptno,
       ename,
       sal,
       hiredate,
       min(sal) keep(dense_rank first order by hiredate) over(partition by deptno) min_sal,
       max(sal) keep(dense_rank last order by hiredate) over(partition by deptno) max_sal
  from emp;

我一开始改错了,被这哥们喷菜鸡,我草。

复制代码
-- 错误等价改写,逻辑不等价
with x as (
select e1.deptno,
       e1.ename,
       e1.sal,
       e1.hiredate,
       row_number() over (partition by DEPTNO order by HIREDATE) rn_first,
       row_number() over (partition by DEPTNO order by HIREDATE DESC) rn_last
from EMP e1)
select
    e.deptno,
    e.ename,
    e.sal,
    e.hiredate,
    x1.SAL,
    x2.SAL
from emp e
    inner join x x1 on e.DEPTNO = x1.DEPTNO and x1.rn_first = 1
    inner join x x2 on e.DEPTNO = x2.DEPTNO and x2.rn_last = 1;

我换了张表测试下,发现上面改写是逻辑有问题,如果同一个组内有相同日期的分组字段内有NULL值的,确实会导致SQL结果集不一致。

复制代码
-- 将EMP表替换成EMPLOYEES,如果使用上面等价改写就错误了。
select DEPARTMENT_ID,
       FIRST_NAME,
       SALARY,
       HIRE_DATE,
       min(SALARY) keep(dense_rank first order by HIRE_DATE) over(partition by DEPARTMENT_ID) min_sal,
       max(SALARY) keep(dense_rank last order by HIRE_DATE) over(partition by DEPARTMENT_ID) max_sal
from EMPLOYEES;

最终等价改写的SQL,增加了分组字段内有NULL值的逻辑和处理一个组内有相同日期的逻辑。

复制代码
select e.DEPARTMENT_ID,
       e.FIRST_NAME,
       e.SALARY,
       e.HIRE_DATE,
       (select MIN_SALARY
        from (select DEPARTMENT_ID, MIN(SALARY) MIN_SALARY
              from (select DEPARTMENT_ID,
                           SALARY,
                           HIRE_DATE,
                           dense_rank() over (PARTITION BY DEPARTMENT_ID ORDER BY HIRE_DATE) RN
                    from EMPLOYEES)
              WHERE RN = 1
              GROUP BY DEPARTMENT_ID) e1
        where case when e1.DEPARTMENT_ID is null then 99999 else e1.DEPARTMENT_ID end = case when e.DEPARTMENT_ID is null then 99999 else e.DEPARTMENT_ID end) a_min,
       (select MAX_SALARY
        from (select DEPARTMENT_ID, MAX(SALARY) MAX_SALARY
              from (select DEPARTMENT_ID,
                           SALARY,
                           HIRE_DATE,
                           dense_rank() over (PARTITION BY DEPARTMENT_ID ORDER BY HIRE_DATE DESC) RN
                    from EMPLOYEES)
              WHERE RN = 1
              GROUP BY DEPARTMENT_ID) e1
        where case when e1.DEPARTMENT_ID is null then 99999 else e1.DEPARTMENT_ID end = case when e.DEPARTMENT_ID is null then 99999 else e.DEPARTMENT_ID end ) a_max
FROM EMPLOYEES e;

差集比较后是等价的:

相关推荐
老苏畅谈运维9 小时前
MySQL性能分析的“秘密武器”,深度剖析SQL问题
数据库·sql·mysql
徐一闪_BigData17 小时前
FlinkSql使用中rank/dense_rank函数报错空指针
大数据·sql·flink
Leven19952717 小时前
Flink (十二) :Table API & SQL (一) 概览
大数据·sql·flink
monstercl1 天前
Ubuntu20彻底删除MySQL8
sql·mysql·ubuntu·c/c++
羊小猪~~1 天前
MYSQL学习笔记(五):单行函数(字符串、数学、日期时间、条件判断、信息、加密、进制转换函数)讲解
数据库·笔记·后端·sql·学习·mysql·考研
羊小猪~~1 天前
MYSQL学习笔记(六):聚合函数、sql语句执行原理简要分析
java·数据库·c++·后端·sql·mysql·考研
MrZhangBaby2 天前
SQL-leetcode—1148. 文章浏览 I
数据库·sql·leetcode
精通HelloWorld!2 天前
金额存储类型-浮点数的精度丢失问题
java·数据库·sql·mysql·big
web150854159352 天前
大模型语料库的构建过程 包括知识图谱构建 垂直知识图谱构建 输入到sql构建 输入到cypher构建 通过智能体管理数据生产组件
sql·知识图谱·easyui
北顾南栀倾寒2 天前
[MySQL]数据库表内容的增删查改操作大全
开发语言·数据库·sql·mysql