窗口函數是 SQL2003 標準才開始有的一系列 SQL 函數,用於應付一些複雜運算是比較方便。但是普遍使用的 MySQL 資料庫對窗口函數支持得卻很不好,直到最近的版本才開始有部分支持,這當然就讓 MySQL 程序員很鬱悶了。
實際操作中,我們可以在 MySQL 里用 SQL 拼出窗口函數功能,但是需要使用用戶變數以及多個 SELECT 表達式從左到右依次計算的隱含規則。下面我們來看兩個例子(為調試方便,我們直接用集算器作為測試環境)。
1、2016 年 1 月銷售額排名
(1)A1 中語句用於初始化用戶變數;
(2)A2 中語句先對銷售額排倒序,然後每一行銷售額與上一行銷售額比較,若相等則排名不變,否則排名等於行號;
(3)A3 連接資料庫;
(4)A4 執行初始化語句;
(5)A5 執行查詢語句並關閉資料庫連接,返回結果。
執行後 A5 為需要的結果。
2、2016 年 1 月和 2 月銷售額按月分組百分比排名
(2)A2 中語句子查詢 t11 求出上一行的月份和銷售額,t1 再求出本月行號與排名,t2 算出每月的行數,最後 t1 與 t2 連接再利用公式 [if(本月行數>1,(當前行的本月排名 -1)/(本組行數 -1),0)] 求出百分比排號。
通過上述兩個例子,我們可以看到,為了實現窗口函數相應功能,SQL 語句冗長、複雜而且可讀性較差。另外,這裡還使用了 SELECT 表達式從左到右依次計算的隱含規則,而這在 MySQL 參考手冊是不推薦使用的,如果今後不能使用這一規則,那麼寫出來的 SQL 語句會更加複雜。譬如不使用這條隱含規則如何能取上一行的欄位值呢?各位讀者可以自行腦補。
值得慶幸的是,有了集算器及其特有的 SPL 語言,我們就大可不必這麼麻煩了,MySQL 只要使用最基本的 SQL 就行了,剩下的事由集算器來完成。
下面我們就來看看集算器的 SPL 語法是如何實現相應窗口函數的功能的。
1、SUM()、COUNT()、AVG()、MAX()、MIN()、VARIANCE
a)
select province, sales, sum(sales) over() `sum`, avg(sales) over() `avg`, max(sales) over() `max`, min(sales) over() `min`, count(*) over() `count` from detail where yearmonth=201601 order by sales;
(1)A3 到 A7 依次對銷售額求和、求平均、求最大、求最小及求總行數;
(2)A8 構造序表,其中每一行都有本月銷售額總和、平均值、最大值、最小值及總行數
執行後 A8 的結果如下:
這個例子很常規,毫無挑戰性,只是小練一把,下面開始玩真的。
b)
select yearmonth,province,sales, sum(sales) over (partition by yearmonth) `sum`, avg(sales) over (partition by yearmonth) `avg`, max(sales) over (partition by yearmonth) `max`, min(sales) over (partition by yearmonth) `min`, count(*) over (partition by yearmonth) `count` from detail where yearmonth in (201601,201602) and sales>49500 order by yearmonth, sales desc;
(1)A2 中按月份分組並對銷售額求和、求平均、求最大、求最小及每組行數;
(2)A4 按月份將 A2 中 yearmonth 欄位值轉換成 A3 中相同月份的記錄
執行後 A5 的結果如下。
2、VARIANCE()、STD()
select province, sales, variance(sales) over() `variance`, std(sales) over() `std` from detail where yearmonth=201601;
(1)A3 對銷售額求方差。
(2)A4 對 A3 求平方根即為標準差
select yearmonth, province, sales, variance(sales) over(partition by yearmonth) `variance`, std(sales) over(partition by yearmonth) `std` from detail where yearmonth in (201601, 201602);
(1)A3 按月份分組
(2)A4 求每月銷售額的方差
執行後 A6 的結果如下:
3、ROW_NUMBER()、RANK()、DENSE_RANK()、PERCENT_RANK()
select province, sales, row_number() over(order by sales desc) `row_number`, rank() over (order by sales desc) `rank`, dense_rank() over (order by sales desc) `dense_rank`, percent_rank() over (order by sales desc) `percent_rank` from detail where yearmonth=201601;
(1)A5 中 #表示當前行在 A3 中的序號
(2) 百分比排名的公式 =if(行數 >1,( 排名 -1)/(行數 -1))
執行後 A5 的結果如下:
select province, sales, row_number() over(partition by yearmonth order by sales desc) `row_number`, rank() over (partition by yearmonth order by sales desc) `rank`, dense_rank() over (partition by yearmonth order by sales desc) `dense_rank`, percent_rank() over (partition by yearmonth order by sales desc) `percent_rank` from detail where yearmonth in (201601,201602);
4、NTILE()
select province, sales, ntile(3) over() `ntile` from detail where yearmonth=201601;
(1)A3 里指明桶數為 3
(2)A5 中 z(i, 桶數, 總行數) 計算第 i 行所在桶號
執行後 A9 的結果如下:
select yearmonth, province, sales, ntile(3) over(partition by yearmonth) `ntile` from detail where yearmonth=201601 or( yearmonth=201602 and province!=上海);
5、FIRST_VALUE()、LAST_VALUE()、NTH_VALUE()、LAG()、LEAD()
select province,sales, first_value(sales) over(partition by yearmonth) `first_value`, last_value(sales) over(partition by yearmonth) `last_value`, nth_value(sales, 5) over(partition by yearmonth) `nth_value`, lag(sales, 2) over(partition by yearmonth) `lag`, lead(sales, 3) over(partition by yearmonth) `lead` from detail where yearmonth=201601;
(1)Am(i) 取 A2 中第 i 條記錄,越界返回 null,負數則從後往前數第 abs(i) 條記錄,不能使用 A2(i),因為 A2(i) 越界會報錯
執行後 A3 的結果如下:
select yearmonth,province,sales, first_value(sales) over(partition by yearmonth) `first_value`, last_value(sales) over(partition by yearmonth) `last_value`, nth_value(sales, 5) over(partition by yearmonth) `nth_value`, lag(sales, 2) over(partition by yearmonth) `lag`, lead(sales, 3) over(partition by yearmonth) `lead` from detail where yearmonth=201601 or (yearmonth=201602 and sales>50000);
(1)A5 中,seq(yearmonth) 儘可能不要在 if 函數中使用,因為 seq 函數是在對 A2 中記錄循環過程中累加的,導致 seq 函數少執行 1 次就少累加 1。
(2)A5 中,前面的表達式用 seq=seq(yearmonth) 對變數 seq 賦值,這樣後續表達式就可以引用變數 seq。
6、CUME_DIST()
select province,sales, cume_dist() over(order by sales) `cume_dist` from detail where yearmonth=201601;
(1)CUME_DIST()over (order by sales) 求銷售額從小到大的累積概率分布,公式為 (小於等於當前銷售額的行數 / 總行數)
(2) 小於等於當前銷售額的行數 = 總行數 - 當前銷售額從大到小的排名 +1
(3)A2 必須按銷售額從大到小排序
(4)A5 數據倒排
select yearmonth, province,sales, cume_dist() over(partition by yearmonth order by sales) `cume_dist` from detail where yearmonth in (201601,201602);
(1) 對應於最後的倒排,A2 中按月份從大到小排序
看完十多個例子,有沒有覺得集算器代碼實現 so easy?!而且,由於集算器可以對單元格進行分步計算,我們可以按照自然的思路逐步查看查詢結果,從而更加簡便、直觀地完善整個查詢腳本。趕緊用起來吧,你會發現更多又方便又強大的功能!