码迷,mamicode.com
首页 > 数据库 > 详细

T-SQL:qualify和window 使用(十七)

时间:2018-12-16 19:29:38      阅读:694      评论:0      收藏:0      [点我收藏+]

标签:row   选项   order   avg   nbsp   rom   win   esc   run   

1.qualify

是一个潜在的额外筛选器

主要用于对开窗函数的数据筛选

SELECT orderid, orderdate, val,
  RANK() OVER(ORDER BY val DESC) AS rnk
FROM Sales.OrderValues
QUALIFY rnk <= 5;

标准SQL没用定义qualify子句,它是Teredata特有的特性。

2.window

允许我们对窗口进行命名描述;然后在定义其他窗口-即将被串钩函数使用或用来定义另一个命名窗口时,代指这个命名的窗口描述。

如下

SELECT empid, ordermonth, qty,
  SUM(qty) OVER (PARTITION BY empid
                 ORDER BY ordermonth
                 ROWS BETWEEN UNBOUNDED PRECEDING
                          AND CURRENT ROW) AS run_sum_qty,
  AVG(qty) OVER (PARTITION BY empid
                 ORDER BY ordermonth
                 ROWS BETWEEN UNBOUNDED PRECEDING
                          AND CURRENT ROW) AS run_avg_qty,
  MIN(qty) OVER (PARTITION BY empid
                 ORDER BY ordermonth
                 ROWS BETWEEN UNBOUNDED PRECEDING
                          AND CURRENT ROW) AS run_min_qty,
  MAX(qty) OVER (PARTITION BY empid
                 ORDER BY ordermonth
                 ROWS BETWEEN UNBOUNDED PRECEDING
                          AND CURRENT ROW) AS run_max_qty
FROM EmpOrders;

用window缩写前置查询

SELECT empid, ordermonth, qty,
  SUM(qty) OVER W1 AS run_sum_qty,
  AVG(qty) OVER W1 AS run_avg_qty,
  MIN(qty) OVER W1 AS run_min_qty,
  MAX(qty) OVER W1 AS run_max_qty
FROM Sales.EmpOrders
WINDOW W1 AS ( PARTITION BY empid
               ORDER BY ordermonth
               ROWS BETWEEN UNBOUNDED PRECEDING
                        AND CURRENT ROW );

window吧一个带有分区,排序和框架选项的完整的窗口描述为w1  

T-SQL:qualify和window 使用(十七)

标签:row   选项   order   avg   nbsp   rom   win   esc   run   

原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/10127532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!