mysql實現oracle的lead和lag函式功能
MariaDB [test]> select * from t;
+--------------------------------------+---------------------+
| case_id | assigned_date |
+--------------------------------------+---------------------+
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:08:14 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-18 14:25:22 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:26:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:19:13 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:53:09 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-19 08:18:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 12:12:35 |
+--------------------------------------+---------------------+
7 rows in set (0.00 sec)
MariaDB [test]> SELECT t1.case_id, t1.assigned_date,
-> (SELECT t2.assigned_date
-> FROM t AS t2
-> WHERE t2.case_id = t1.case_id AND
-> t2.assigned_date > t1.assigned_date
-> ORDER BY t2.assigned_date LIMIT 1) AS next_date,
-> (SELECT t2.assigned_date
-> FROM t AS t2
-> WHERE t2.case_id = t1.case_id AND
-> t2.assigned_date < t1.assigned_date
-> ORDER BY t2.assigned_date DESC LIMIT 1) AS prev_date
-> FROM t AS t1;
+--------------------------------------+---------------------+---------------------+---------------------+
| case_id | assigned_date | next_date | prev_date |
+--------------------------------------+---------------------+---------------------+---------------------+
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:08:14 | 2016-05-18 14:26:01 | NULL |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-18 14:25:22 | 2016-05-19 08:18:01 | NULL |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:26:01 | 2016-05-19 07:19:13 | 2016-05-18 14:08:14 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:19:13 | 2016-05-19 07:53:09 | 2016-05-18 14:26:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:53:09 | 2016-05-19 12:12:35 | 2016-05-19 07:19:13 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-19 08:18:01 | NULL | 2016-05-18 14:25:22 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 12:12:35 | NULL | 2016-05-19 07:53:09 |
+--------------------------------------+---------------------+---------------------+---------------------+
7 rows in set (0.01 sec)
+--------------------------------------+---------------------+
| case_id | assigned_date |
+--------------------------------------+---------------------+
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:08:14 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-18 14:25:22 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:26:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:19:13 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:53:09 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-19 08:18:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 12:12:35 |
+--------------------------------------+---------------------+
7 rows in set (0.00 sec)
MariaDB [test]> SELECT t1.case_id, t1.assigned_date,
-> (SELECT t2.assigned_date
-> FROM t AS t2
-> WHERE t2.case_id = t1.case_id AND
-> t2.assigned_date > t1.assigned_date
-> ORDER BY t2.assigned_date LIMIT 1) AS next_date,
-> (SELECT t2.assigned_date
-> FROM t AS t2
-> WHERE t2.case_id = t1.case_id AND
-> t2.assigned_date < t1.assigned_date
-> ORDER BY t2.assigned_date DESC LIMIT 1) AS prev_date
-> FROM t AS t1;
+--------------------------------------+---------------------+---------------------+---------------------+
| case_id | assigned_date | next_date | prev_date |
+--------------------------------------+---------------------+---------------------+---------------------+
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:08:14 | 2016-05-18 14:26:01 | NULL |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-18 14:25:22 | 2016-05-19 08:18:01 | NULL |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-18 14:26:01 | 2016-05-19 07:19:13 | 2016-05-18 14:08:14 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:19:13 | 2016-05-19 07:53:09 | 2016-05-18 14:26:01 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 07:53:09 | 2016-05-19 12:12:35 | 2016-05-19 07:19:13 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d51 | 2016-05-19 08:18:01 | NULL | 2016-05-18 14:25:22 |
| 41c19f76-e52e-c4c9-62c2-573c71ec5d50 | 2016-05-19 12:12:35 | NULL | 2016-05-19 07:53:09 |
+--------------------------------------+---------------------+---------------------+---------------------+
7 rows in set (0.01 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10972173/viewspace-2122063/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle函式之lag和leadOracle函式
- oracle中lead和lag函式Oracle函式
- lead和lag函式函式
- lag和lead函式函式
- oracle之函式LAG,LEADOracle函式
- 【分析函式】Oracle分析函式之LAG和LEAD函式Oracle
- Oracle分析函式之LEAD和LAG實際應用Oracle函式
- 使用LAG和LEAD函式統計函式
- ORALCE函式:LAG()和LEAD() 分析函式詳解函式
- oracle lag與lead分析函式簡介Oracle函式
- [Oracle]高效的SQL語句之分析函式(四)--lag()/lead()OracleSQL函式
- Hive 分析函式lead、lag例項應用Hive函式
- oracle中LAG()和LEAD()等分析統計函式的用法(統計月增長率)Oracle函式
- 分析函式 - LAG函式
- 【Mysql】Mysql似oracle分析函式sum over的實現MySqlOracle函式
- lead lag rank()over()dense_rank()row_number()over()rollupcube幾大分析函式函式
- MySQL 函式索引功能終於可以實現了MySql函式索引
- 關於lag函式的用法函式
- MySQL排名函式實現MySql函式
- MySQL分析函式實現MySql函式
- mysql實現開窗函式MySql函式
- C#函式實現的小功能集合C#函式
- Oracle分析函式RANK(),ROW_NUMBER(),LAG()等的使用方法(轉)Oracle函式
- 利用指標實現strncmp函式功能指標函式
- 13、MySQL Case-MySQL分析函式實現MySql函式
- 不使用日期函式實現ADD_MONTHS函式功能函式
- 不使用日期函式實現ADD_MONTHS函式功能(二)函式
- MySQL空間函式實現位置打卡MySql函式
- [PY3]——函式——函式註解 | 實現型別檢查功能函式型別
- (函式)實現strstr函式函式
- mysql與oracle的分組函式MySqlOracle函式
- mysql和oracle字串編碼轉換函式,字串轉位元組函式例子MySqlOracle字串編碼函式
- oracle的instr函式在hive上面的實現Oracle函式Hive
- memmove和memcpy函式的區別及實現memcpy函式
- 用ORACLE分析函式實現行列轉換Oracle函式
- oracle 與 mysql 中的函式總結OracleMySql函式
- MySQL 的日期和時間函式MySql函式
- MySQL 的IFNULL()、ISNULL()和NULLIF()函式MySqlNull函式