资格与

时间:2015-10-27 05:55:12

标签: teradata

有人可以解释一下,Teradata中qualify...over...partition bygroup by...having之间的区别是什么?
我也想知道他们的表现是否存在差异。

2 个答案:

答案 0 :(得分:14)

QUALIFY是一个专有扩展,用于过滤窗口化聚合函数的结果。

按特定顺序对查询进行逻辑处理:

  1. FROM:创建基本结果集
  2. WHERE:从上一个结果集中删除行
  3. GROUP BY:对前一个结果集应用聚合函数
  4. HAVING:从上一个结果集中删除行
  5. OVER:在先前的结果集上应用窗口化聚合函数
  6. QUALIFY:从上一个结果集中删除行

答案 1 :(得分:2)

Having clause is used to filter the result set of the aggregate functions like (COUNT,min,max etc)
they eliminate rows based from groups based on some criteria like this :-
SELECT dept_no, MIN(salary), MAX(salary), AVG(salary)
     FROM employee
     WHERE dept_no IN (100,300,500,600)
     GROUP BY dept_no
     HAVING AVG(salary) > 37000;

The QUALIFY clause eliminates rows based on the function value, returning a new value for each of the participating rows.
It works on the final result set.

SELECT NAME,LOCATION FROM EMPLOYEE
 QUALIFY ROW_NUMBER() OVER ( PARTITION BY NAME ORDER BY JOINING_DATE DESC) = 1;


We can club both having and qualify as well in a query if we use both aggregate and analytical fucntion like below:-
SELECT StoreID, SUM(sale),
   SUM(profit) OVER (PARTITION BY StoreID)
   FROM facts
   GROUP BY StoreID, sale, profit
   HAVING SUM(sale) > 15
   QUALIFY SUM(profit) OVER (PARTITION BY StoreID) > 2;   



You can see there order of execution from dnoeth answer.