在Oracle中有和没有KEEP的PARTITION BY

时间:2013-11-22 13:20:47

标签: sql oracle

我遇到了两个似乎有相同结果的查询:在分区上应用聚合函数。

我想知道这两个查询之间是否有任何区别:

SELECT empno,
   deptno,
   sal,
   MIN(sal) OVER (PARTITION BY deptno) "Lowest",
   MAX(sal) OVER (PARTITION BY deptno) "Highest"
FROM empl

SELECT empno,
   deptno,
   sal,
   MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) "Lowest",
   MAX(sal) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno) "Highest"
FROM empl

第一个版本更合乎逻辑,但第二个版本可能是某种特殊情况,也许是一些性能优化。

5 个答案:

答案 0 :(得分:24)

MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno)

该陈述可以(从大致)从右到左的顺序来考虑:

  • OVER (PARTITION BY deptno)表示将行划分为deptno的不同组;然后
  • ORDER BY sal表示,对于每个分区,按sal排序行(隐式使用ASC结束顺序);然后
  • KEEP (DENSE_RANK FIRST表示为每个分区的有序行提供(连续)排名(对于排序列具有相同值的行将被赋予相同的排名)并丢弃所有未排在第一位的行;最后
  • MIN(sal)表示每个分区的剩余行,返回最低工资。

在这种情况下,MINDENSE_RANK FIRST都在sal列上运行,因此会执行相同的操作,KEEP (DENSE_RANK FIRST ORDER BY sal)也是多余的。

但是,如果您使用不同的列作为最小值,那么您可以看到效果:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test (name, sal, deptno) AS
SELECT 'a', 1, 1 FROM DUAL
UNION ALL SELECT 'b', 1, 1 FROM DUAL
UNION ALL SELECT 'c', 1, 1 FROM DUAL
UNION ALL SELECT 'd', 2, 1 FROM DUAL
UNION ALL SELECT 'e', 3, 1 FROM DUAL
UNION ALL SELECT 'f', 3, 1 FROM DUAL
UNION ALL SELECT 'g', 4, 2 FROM DUAL
UNION ALL SELECT 'h', 4, 2 FROM DUAL
UNION ALL SELECT 'i', 5, 2 FROM DUAL
UNION ALL SELECT 'j', 5, 2 FROM DUAL;

查询1

SELECT DISTINCT
  MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) AS min_sal_first_sal,
  MAX(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) AS max_sal_first_sal,
  MIN(name) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) AS min_name_first_sal,
  MAX(name) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) AS max_name_first_sal,
  MIN(name) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno) AS min_name_last_sal,
  MAX(name) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno) AS max_name_last_sal,
  deptno
FROM test

<强> Results

| MIN_SAL_FIRST_SAL | MAX_SAL_FIRST_SAL | MIN_NAME_FIRST_SAL | MAX_NAME_FIRST_SAL | MIN_NAME_LAST_SAL | MAX_NAME_LAST_SAL | DEPTNO |
|-------------------|-------------------|--------------------|--------------------|-------------------|-------------------|--------|
|                 1 |                 1 |                  a |                  c |                 e |                 f |      1 |
|                 4 |                 4 |                  g |                  h |                 i |                 j |      2 |

答案 1 :(得分:3)

在您的示例中,没有区别,因为您的聚合位于您要排序的同一列上。 “KEEP”的实际点/功能是在不同的列上进行聚合和排序时。例如(从另一个答案中借用“测试”表)......

SELECT deptno,  min(name) keep ( dense_rank first order by sal desc, name  ) ,
max(sal)
FROM test
group by deptno

此查询获取每个部门中薪水最高的人的姓名。考虑没有“KEEP”子句的替代方案:

SELECT deptno, name, sal
FROM test t
WHERE not exists ( SELECT 'person with higher salary in same department'
                                            FROM test t2  
                                            WHERE t2.deptno = t.deptno
                                            and ((  t2.sal > t.sal )
                                            OR ( t2.sal = t.sal AND t2.name < t.name ) ) )

KEEP子句更容易,更有效(在这个简单的例子中,只有3个一致的获得,而34个获得替代)。

答案 2 :(得分:0)

要详细说明@ MT0答案中提到的一种区别: 在您的第一个查询中,聚合函数MIN和MAX在完成任务, 而在第二秒中,实际的行将由FIRST,LAST和KEEP进行选择。

在第二个示例中,您甚至可以用MIN代替MAX,它将仍然给出正确的答案(最高薪水)。

有关更多信息,请参阅以下 article

答案 3 :(得分:-1)

如果您根据两列进行排序并获取其中一列或两列,也会有所帮助。

CREATE TABLE test (name, sal, deptno) AS
SELECT 'adam', 100, 1 FROM DUAL
UNION ALL SELECT 'bravo', 500, 1 FROM DUAL
UNION ALL SELECT 'coy', 456, 1 FROM DUAL
UNION ALL SELECT 'david', 50, 1 FROM DUAL
UNION ALL SELECT 'ethan', 50, 1 FROM DUAL
UNION ALL SELECT 'feral', 300, 1 FROM DUAL;

现在您要选择薪水最低的员工以及人员的薪水。条件是,如果两名员工具有相同的最低薪水,则按名称首先按字母顺序获取一名员工。

  select o.deptno
,min(o.sal) keep 
  (dense_rank first order by o.sal, o.name) least_salary
,min(o.name) keep 
  (dense_rank first order by o.sal, o.name) least_salary_person
 from test o
  group by 
 o.deptno;

输出:

DEPTNO LEAST_SALARY LEAST_SALARY_PERSON

1 50 david

答案 4 :(得分:-2)

此查询获取每个部门中薪水最高的人的姓名。

select MIN(ename),sal,deptno
from emp where sal in
   (
    select max(sal) from emp group by deptno
   )
GROUP BY sal,deptno;