MySQL8.0實現(xiàn)窗口函數(shù)計算同比環(huán)比
我們在業(yè)務中常常需要統(tǒng)計這個月銷售量多少,同比增加多少,環(huán)比增加多少。這篇博文我們就看看如何利用窗口函數(shù)實現(xiàn)同比及環(huán)比的計算。
用到的關鍵字包括:lag, window
準備工作
首先我們得有MySQL 8.0及以上版本, 然后我們準備一張統(tǒng)計表。
CREATE TABLE `my_stat` ( `month` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `profit` decimal(10,2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
表里的內容如下:
month | profit |
---|---|
2022-05 | 10.00 |
2022-06 | 12.00 |
2022-07 | 6.00 |
2022-08 | 30.00 |
2022-09 | 20.00 |
2022-10 | 8.00 |
2022-11 | 2.00 |
2022-12 | 4.00 |
2023-01 | 14.00 |
2023-02 | 10.00 |
2023-03 | 8.00 |
2023-04 | 9.00 |
2023-05 | 7.00 |
2023-06 | 10.00 |
2023-07 | 15.00 |
數(shù)據(jù) 插入的SQL
INSERT INTO `my_stat` (`month`, `profit`) VALUES ('2022-05', 10.00), ('2022-06', 12.00), ('2022-07', 6.00), ('2022-08', 30.00), ('2022-09', 20.00), ('2022-10', 8.00), ('2022-11', 2.00), ('2022-12', 4.00), ('2023-01', 14.00), ('2023-02', 10.00), ('2023-03', 8.00), ('2023-04', 9.00), ('2023-05', 7.00), ('2023-06', 10.00), ('2023-07', 15.00);
環(huán)比計算
先看最終SQL及輸出結果
select `month`,profit, lag(profit) OVER w AS 上月, (profit - lag(profit) OVER w) as 環(huán)比, 100*(profit - lag(profit) OVER w)/lag(profit) OVER w as 環(huán)比比例 from my_stat WINDOW w AS (ORDER BY `month`);
輸出結果:
month|profit|上月|環(huán)比|環(huán)比比例
--|--|--|--|--
2022-05|10.00|NULL|NULL|NULL
2022-06|12.00|10.00|2.00|20.000000
2022-07|6.00|12.00|-6.00|-50.000000
2022-08|30.00|6.00|24.00|400.000000
2022-09|20.00|30.00|-10.00|-33.333333
2022-10|8.00|20.00|-12.00|-60.000000
2022-11|2.00|8.00|-6.00|-75.000000
2022-12|4.00|2.00|2.00|100.000000
2023-01|14.00|4.00|10.00|250.000000
2023-02|10.00|14.00|-4.00|-28.571429
2023-03|8.00|10.00|-2.00|-20.000000
2023-04|9.00|8.00|1.00|12.500000
2023-05|7.00|9.00|-2.00|-22.222222
2023-06|10.00|7.00|3.00|42.857143
2023-07|15.00|10.00|5.00|50.000000
lag
lag 函數(shù)的完整定義如下:
LAG(expr [, N[, default]]) [null_treatment] over_clause
- 參數(shù)一 expr(表達式)是必須的,這里可以是字段名,如上面的示例SQL,也可以是其它運算表達式。
- 參數(shù)二 N 是可選的,必須是大于等于0的整數(shù)。默認1,1表示上一行,0表示當前行,12表示前12行,后面計算同比時會用到
- 參數(shù)三 是默認值,比如第一行的 上一行是不存在的,這個時候 返回什么值就可以通過參數(shù)三來控制,默認是 NULL
- null_treatment 的定義是處理NULL的策略,但是因為MySQL只實現(xiàn)了 RESPECT NULLS 并且作為默認值,就感覺它只是提醒我們 計算結果需要考慮NULL
over_clause 是必須的,它描述窗口的定義, 如上面的SQL示例最后一行:
WINDOW w AS (ORDER BY `month`);
這里定義了一個 窗口(window), 基于 month這個字段升序。
window
上一節(jié)的 over_clause 定義如下:
over_clause: {OVER (window_spec) | OVER window_name}
我們看到,可以 over (窗口定義) 或者 over 窗口名稱。
lag(profit) OVER w AS 上月
這就是一個 over 窗口名稱的示例。
接下來我們看 window_spec 的定義:
window_spec: [window_name] [partition_clause] [order_clause] [frame_clause]
- window_name 就是給窗口起個名字,方便使用
- partition_clause 分區(qū)定義, 就是定義查詢結果如何分組,有點類似group by 的意思。
- order_clause 排序定義,定義窗口內容排序字段及方式,如前面示例里的 order by `month` 。如果省略了order by , 那就按查詢到結果的順序來排序。
- frame_clause frame_clause(框架子句)指定如何定義子集
同比環(huán)比計算
還是先看最終SQL
select `month`,profit, lag(profit) OVER w AS 上月, (profit - lag(profit) OVER w) as 環(huán)比, 100*(profit - lag(profit) OVER w)/lag(profit) OVER w as 環(huán)比比例, lag(profit,12) OVER w AS 上年同月, (profit - lag(profit,12) OVER w) as 同比, 100*(profit - lag(profit,12) OVER w)/lag(profit,12) OVER w as 同比比例 from my_stat WINDOW w AS (ORDER BY `month`);
返回結果
month | profit | 上月 | 環(huán)比 | 環(huán)比比例 | 上年同月 | 同比 | 同比比例 |
---|---|---|---|---|---|---|---|
2022-05 | 10.00 | NULL | NULL | NULL | NULL | NULL | NULL |
2022-06 | 12.00 | 10.00 | 2.00 | 20.000000 | NULL | NULL | NULL |
2022-07 | 6.00 | 12.00 | -6.00 | -50.000000 | NULL | NULL | NULL |
2022-08 | 30.00 | 6.00 | 24.00 | 400.000000 | NULL | NULL | NULL |
2022-09 | 20.00 | 30.00 | -10.00 | -33.333333 | NULL | NULL | NULL |
2022-10 | 8.00 | 20.00 | -12.00 | -60.000000 | NULL | NULL | NULL |
2022-11 | 2.00 | 8.00 | -6.00 | -75.000000 | NULL | NULL | NULL |
2022-12 | 4.00 | 2.00 | 2.00 | 100.000000 | NULL | NULL | NULL |
2023-01 | 14.00 | 4.00 | 10.00 | 250.000000 | NULL | NULL | NULL |
2023-02 | 10.00 | 14.00 | -4.00 | -28.571429 | NULL | NULL | NULL |
2023-03 | 8.00 | 10.00 | -2.00 | -20.000000 | NULL | NULL | NULL |
2023-04 | 9.00 | 8.00 | 1.00 | 12.500000 | NULL | NULL | NULL |
2023-05 | 7.00 | 9.00 | -2.00 | -22.222222 | 10.00 | -3.00 | -30.000000 |
2023-06 | 10.00 | 7.00 | 3.00 | 42.857143 | 12.00 | -2.00 | -16.666667 |
2023-07 | 15.00 | 10.00 | 5.00 | 50.000000 | 6.00 | 9.00 | 150.000000 |
同比與環(huán)比的差別其實不大,只是 lag(profit)變成了 lag(profit,12)。 lag(profit,12)表示取前12行的數(shù)據(jù)。
需要注意的是計算同比的時候,我們需要保證數(shù)據(jù)是連續(xù)的,不然數(shù)據(jù)會有偏差,因為這里的lag(profit,12)取的是前12行的數(shù)據(jù),如果月份數(shù)據(jù)有缺失就可以取錯數(shù)據(jù)。比如:如果少了一個月,那么前12行取的可能就是去年上個月的數(shù)據(jù)了。
partition_clause 聊聊分區(qū)
我們先準備一下數(shù)據(jù),先創(chuàng)建一張表 my_stat_food
CREATE TABLE `my_stat_food` ( `month` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, `profit` decimal(10,2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
然后插入數(shù)據(jù)
INSERT INTO `my_stat_food` (`month`, `type`, `profit`) VALUES ('2023-05', '蔬菜', 1.00), ('2023-05', '水果', 2.00), ('2023-05', '肉類', 3.00), ('2023-06', '蔬菜', 4.00), ('2023-06', '水果', 5.00), ('2023-06', '肉類', 6.00), ('2023-07', '蔬菜', 7.00), ('2023-07', '水果', 8.00), ('2023-07', '肉類', 9.00);
定義
partition_clause: PARTITION BY expr [, expr] ...
然后我們試試pratition的效果
select *,lag(`profit`) over (partition by `type`) as lp from my_stat_food
輸出:
month | type | profit | lp |
---|---|---|---|
2023-05 | 水果 | 2.00 | NULL |
2023-06 | 水果 | 5.00 | 2.00 |
2023-07 | 水果 | 8.00 | 5.00 |
2023-05 | 肉類 | 3.00 | NULL |
2023-06 | 肉類 | 6.00 | 3.00 |
2023-07 | 肉類 | 9.00 | 6.00 |
2023-05 | 蔬菜 | 1.00 | NULL |
2023-06 | 蔬菜 | 4.00 | 1.00 |
2023-07 | 蔬菜 | 7.00 | 4.00 |
我們的數(shù)據(jù)是按月分排序的,但我們看到通過 partition進行分區(qū)后,就會分區(qū)來返回內容了。這樣我們就可以通過分區(qū)來統(tǒng)計不同品類的環(huán)比。
問題
- 上面的給數(shù)據(jù)都是按月統(tǒng)計好的數(shù)據(jù)。但原始數(shù)據(jù)往往是一筆筆訂單,那么如何通過原始數(shù)據(jù)生成每個月的統(tǒng)計數(shù)據(jù)呢?
- 如何保證數(shù)據(jù)的連續(xù)性?在業(yè)務上每個月的數(shù)據(jù)往往是有保證的,畢竟一個月一單不成交的話,也沒有統(tǒng)計的必要了。但是考慮到天呢?可以一些小店一天一個成交也沒有,也是正常的。那么如何實現(xiàn)如果某天沒有數(shù)據(jù),統(tǒng)計的時候讓那天的統(tǒng)計數(shù)據(jù)變成0呢?
參考
https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
到此這篇關于MySQL8.0實現(xiàn)窗口函數(shù)計算同比環(huán)比的文章就介紹到這了,更多相關MySQL 窗口函數(shù)計算內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MySQL數(shù)據(jù)庫優(yōu)化之索引實現(xiàn)原理與用法分析
這篇文章主要介紹了MySQL數(shù)據(jù)庫優(yōu)化之索引實現(xiàn)原理與用法,結合實例形式分析了mysql數(shù)據(jù)庫優(yōu)化操作的索引原理、具體實現(xiàn)與相關操作注意事項,需要的朋友可以參考下2020-01-01