T-SQL:qualify和window 使用(十七)
阿新 • • 發佈:2018-12-16
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 PRECEDINGAND 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 ROWSBETWEEN 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