MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现
原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b
MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现
CREATE TABLE test_rownum (
ID int,
Ke CHAR(1),
val INT
);
INSERT INTO test_rownum
SELECT 1, 'A', 1 UNION ALL
SELECT 2, 'A', 2 UNION ALL
SELECT 3, 'A', 3 UNION ALL
SELECT 4, 'B', 1 UNION ALL
SELECT 5, 'B', 2 UNION ALL
SELECT 6, 'B', 1 UNION ALL
SELECT 7, 'C', 3 UNION ALL
SELECT 8, 'C', 2 UNION ALL
SELECT 9, 'C', 2;
rownum 用于显示序号
ID 字段用于 标记 原有的序号位置.
普通的处理
等价于 ROW_NUMBER() OVER (ORDER BY ke, val)
SELECT
@rownum:=@rownum+1 AS rownum,
id,
ke,
val
FROM
(SELECT @rownum:=0) r,
test_rownum
ORDER BY
ke, val;
+--------+------+------+------+
| rownum | id | ke | val |
+--------+------+------+------+
| 1 | 1 | A | 1 |
| 2 | 2 | A | 2 |
| 3 | 3 | A | 3 |
| 4 | 4 | B | 1 |
| 5 | 6 | B | 1 |
| 6 | 5 | B | 2 |
| 7 | 8 | C | 2 |
| 8 | 9 | C | 2 |
| 9 | 7 | C | 3 |
+--------+------+------+------+
9 rows in set (0.00 sec)
PARTITION 的处理
等价于 ROW_NUMBER() OVER (PARTITION BY ke ORDER BY val)
SELECT
CASE
WHEN @ke != ke THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS rownum,
id,
@ke := ke AS ke,
val
FROM
(SELECT @ke:='') k,
(SELECT @rownum:=0) r,
test_rownum main
ORDER BY
ke, val;
+--------+------+------+------+
| rownum | id | ke | val |
+--------+------+------+------+
| 1 | 1 | A | 1 |
| 2 | 2 | A | 2 |
| 3 | 3 | A | 3 |
| 1 | 4 | B | 1 |
| 2 | 6 | B | 1 |
| 3 | 5 | B | 2 |
| 1 | 8 | C | 2 |
| 2 | 9 | C | 2 |
| 3 | 7 | C | 3 |
+--------+------+------+------+
9 rows in set (0.00 sec)
RANK DENSE_RANK 的处理
等价于 RANK() OVER (PARTITION BY ke ORDER BY val)
等价于 DENSE_RANK() OVER (PARTITION BY ke ORDER BY val)
SELECT
CASE
WHEN @ke != ke THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS ROW_NUMBER,
CASE
WHEN @ke != ke THEN @rank:= 1
WHEN @val = val THEN @rank
ELSE @rank:= @rownum
END AS RANK,
CASE
WHEN @ke != ke THEN @dense_rank:= 1
WHEN @val = val THEN @dense_rank
ELSE @dense_rank:= @dense_rank + 1
END AS DENSE_RANK,
id,
@ke := ke AS ke,
@val := val AS val
FROM
(SELECT @ke:='') k,
(SELECT @val:=0) v,
(SELECT @rownum:=0) r,
(SELECT @rank:=0) r2,
(SELECT @dense_rank:=0) d,
test_rownum main
ORDER BY
ke, val;
+------------+------+------------+------+------+------+
| ROW_NUMBER | RANK | DENSE_RANK | id | ke | val |
+------------+------+------------+------+------+------+
| 1 | 1 | 1 | 1 | A | 1 |
| 2 | 2 | 2 | 2 | A | 2 |
| 3 | 3 | 3 | 3 | A | 3 |
| 1 | 1 | 1 | 4 | B | 1 |
| 2 | 1 | 1 | 6 | B | 1 |
| 3 | 3 | 2 | 5 | B | 2 |
| 1 | 1 | 1 | 8 | C | 2 |
| 2 | 1 | 1 | 9 | C | 2 |
| 3 | 3 | 2 | 7 | C | 3 |
+------------+------+------------+------+------+------+
9 rows in set (0.00 sec)
=======================================================
两表关联情况下的测试.
CREATE TABLE test_rownum_main (
ID int,
Ke CHAR(1)
);
CREATE TABLE test_rownum_sub (
KeID int,
val INT
);
INSERT INTO test_rownum_main
SELECT 1, 'A' UNION ALL
SELECT 2, 'B' UNION ALL
SELECT 3, 'C';
INSERT INTO test_rownum_sub
SELECT 1, 1 UNION ALL
SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 3, 3 UNION ALL
SELECT 3, 2 UNION ALL
SELECT 3, 2;
普通的处理
等价于 ROW_NUMBER() OVER (ORDER BY ke, val)
SELECT
@rownum:=@rownum+1 AS rownum,
main.id,
main.ke,
sub.val
FROM
test_rownum_main main
JOIN test_rownum_sub sub
ON (main.ID = sub.KeID),
(SELECT @rownum:=0) r
ORDER BY
ke, val;
+--------+------+------+------+
| rownum | id | ke | val |
+--------+------+------+------+
| 1 | 1 | A | 1 |
| 2 | 1 | A | 2 |
| 3 | 1 | A | 3 |
| 4 | 2 | B | 1 |
| 6 | 2 | B | 1 |
| 5 | 2 | B | 2 |
| 8 | 3 | C | 2 |
| 9 | 3 | C | 2 |
| 7 | 3 | C | 3 |
+--------+------+------+------+
9 rows in set (0.00 sec)
rownum 顺序不满足预期结果了。
可能是因为执行关联的时候, 就把 @rownum 计算了。
修改SQL语句.
SELECT
@rownum:=@rownum+1 AS rownum,
id,
ke,
val
FROM
(SELECT @rownum:=0) r,
(
SELECT
main.id,
main.ke,
sub.val
FROM
test_rownum_main main
JOIN test_rownum_sub sub
ON (main.ID = sub.KeID)
) subQuery
ORDER BY
ke, val;
+--------+------+------+------+
| rownum | id | ke | val |
+--------+------+------+------+
| 1 | 1 | A | 1 |
| 2 | 1 | A | 2 |
| 3 | 1 | A | 3 |
| 4 | 2 | B | 1 |
| 5 | 2 | B | 1 |
| 6 | 2 | B | 2 |
| 7 | 3 | C | 2 |
| 8 | 3 | C | 2 |
| 9 | 3 | C | 3 |
+--------+------+------+------+
9 rows in set (0.00 sec)
PARTITION 的处理
等价于 ROW_NUMBER() OVER (PARTITION BY ke ORDER BY val)
SELECT
CASE
WHEN @ke != ke THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS rownum,
id,
@ke := ke AS ke,
val
FROM
(SELECT @ke:='') k,
(SELECT @rownum:=0) r,
(
SELECT
main.id,
main.ke,
sub.val
FROM
test_rownum_main main
JOIN test_rownum_sub sub
ON (main.ID = sub.KeID)
) subQuery
ORDER BY
ke, val;
+--------+------+------+------+
| rownum | id | ke | val |
+--------+------+------+------+
| 1 | 1 | A | 1 |
| 2 | 1 | A | 2 |
| 3 | 1 | A | 3 |
| 1 | 2 | B | 1 |
| 2 | 2 | B | 1 |
| 3 | 2 | B | 2 |
| 1 | 3 | C | 2 |
| 2 | 3 | C | 2 |
| 3 | 3 | C | 3 |
+--------+------+------+------+
9 rows in set (0.00 sec)
DENSE_RANK 的处理
RANK 的处理
等价于 DENSE_RANK() OVER (PARTITION BY ke ORDER BY val)
等价于 RANK() OVER (PARTITION BY ke ORDER BY val)
SELECT
CASE
WHEN @ke != ke THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS ROW_NUMBER,
CASE
WHEN @ke != ke THEN @rank:= 1
WHEN @val = val THEN @rank
ELSE @rank:= @rownum
END AS RANK,
CASE
WHEN @ke != ke THEN @dense_rank:= 1
WHEN @val = val THEN @dense_rank
ELSE @dense_rank:= @dense_rank + 1
END AS DENSE_RANK,
id,
@ke := ke AS ke,
@val := val AS val
FROM
(SELECT @ke:='') k,
(SELECT @val:=0) v,
(SELECT @rownum:=0) r,
(SELECT @rank:=0) r2,
(SELECT @dense_rank:=0) d,
(
SELECT
main.id,
main.ke,
sub.val
FROM
test_rownum_main main
JOIN test_rownum_sub sub
ON (main.ID = sub.KeID)
) subQuery
ORDER BY
ke, val;
+------------+------+------------+------+------+------+
| ROW_NUMBER | RANK | DENSE_RANK | id | ke | val |
+------------+------+------------+------+------+------+
| 1 | 1 | 1 | 1 | A | 1 |
| 2 | 2 | 2 | 1 | A | 2 |
| 3 | 3 | 3 | 1 | A | 3 |
| 1 | 1 | 1 | 2 | B | 1 |
| 2 | 1 | 1 | 2 | B | 1 |
| 3 | 3 | 2 | 2 | B | 2 |
| 1 | 1 | 1 | 3 | C | 2 |
| 2 | 1 | 1 | 3 | C | 2 |
| 3 | 3 | 2 | 3 | C | 3 |
+------------+------+------------+------+------+------+
9 rows in set (0.00 sec)
MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现的更多相关文章
- SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: .row_number .rank .dense_rank .ntile 下面分别介绍一下这四个 ...
- ROW_NUMBER、RANK、DENSE_RANK的用法
--NND,索性把2005的一起帖出来. ROW_NUMBER.RANK.DENSE_RANK的用法 (爱新觉罗.毓华 -- 广东深圳) SQL Server 引入几个新的排序(排名)函数,如ROW_ ...
- 四个排名函数(row_number、rank、dense_rank和ntile)的比较
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: 1.row_number 2.rank 3.dense_rank 4.ntile 下面分别介绍一 ...
- SQL中的排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别. 在使用排名函数的时候需要注意以下三点: 1.排名函数必须有 OVER 子句. 2.排名函数必须有包含 ORDE ...
- Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)(转载)
Sql 四大排名函数(ROW_NUMBER.RANK.DENSE_RANK.NTILE)简介 排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张O ...
- hive笔记之row_number、rank、dense_rank
hive中有三个与分组排序相关的分析函数(我起初也认为是窗口函数,后来看到手册里是把他们划到了Analytics functions下),row_number.rank.dense_rank,我一直傻 ...
- SQL With As 用法Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
Sql 四大排名函数(ROW_NUMBER.RANK.DENSE_RANK.NTILE)简介 排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张O ...
- sql server 2000 对应 sql server 2005的row_number()、rank()、DENSE_RANK( )、ntile( )等用法
转自CSDN:http://blog.csdn.net/htl258/article/details/4006717 SQL server 2005新增的几个函数,分别是row_number( ).r ...
- row_number()、rank()、dense_rank()、ntile()
原文:row_number().rank().dense_rank().ntile() SQL2005中row_number()等函数的用法 2005比2000新增了几个函数,分别是row_numbe ...
随机推荐
- Java:网络编程值TCP的使用
演示TCP传输 1.Tcp分客户端和服务端 2.客服端对应的对象是scoket 服务端对应的对象是serverscoket 客户端: 通过查阅scoket对象,发现在建立对象时,就可以连 ...
- .net的远程调用
.Net远程调用(转自:http://www.cnblogs.com/omilan/articles/3191378.html) 看到了这.net远程调用的讲解,觉得不错,拿来分享!! .Net对于远 ...
- tez参数
https://tez.apache.org/releases/0.8.4/tez-api-javadocs/configs/TezConfiguration.html
- 2017.7.21 Linux中ELK服务后台运行方式
通过 2017.7.18 linux下ELK环境搭建 搭建好服务于之后,一旦按下Ctrl+C或者退出登录会话,服务的进程就随之停止了.要长期在后台运行此程序,有几种方式: 1 nohup 输入Ctrl ...
- Bounded Context
From http://martinfowler.com/bliki/BoundedContext.html Bounded Context is a central pattern in Domai ...
- cordova与ios native code交互的原理
非常早曾经写了一篇博客,总结cordova插件怎么调用到原生代码:cordova调用过程,只是写得太水.基本没有提到原理.近期加深了一点理解,又一次补充说明一下 js调用native 以下是我们产品中 ...
- HTTP Analyzer过滤器使用
HTTP Analyzer简单易用,真实抓包居家必备啊,上一次分享了Fiddler的过滤条件,这次介绍下这款软件的过滤,首先按照肯定是按照软件类型分类喽: 1.按照软件过滤: 这样只会显示chrome ...
- Android 网络状态的监控
1 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html 2
- zabbix自动发现监控远程端口
zabbix监控远程服务器端口,simple checks是zabbix用来监控无agent的主机 脚本和模板地址: https://github.com/mikeluwen/tcpmonitor
- 微信公众号开发之创建菜单栏代码示例(php)
思路很简单:就是先获取access_token,然后带着一定规则的json数据参数请求创建菜单的接口.废话不多讲,直接上代码. class Wechat { public $APPID="w ...