IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> mysql 排名 -> 正文阅读

[大数据]mysql 排名

1、不分组排名

1、连续排名

## mysql 8
SELECT
	score,ranking 
FROM
	(
	SELECT
		s.score,
		ROW_NUMBER () OVER ( ORDER BY score DESC ) ranking 
	FROM
		(
		SELECT 0 AS score 
		UNION ALL
		SELECT 0 AS score
	    UNION ALL
		SELECT 0 AS score 
		UNION ALL
		SELECT 1 AS score 
		UNION ALL
		SELECT 2 AS score 
		UNION ALL
		SELECT 1 AS score 
		) s
		ORDER BY
		s.score DESC 
	) a;

## mysql5.7
select score,ranking
from (
	SELECT 
	 s.score,
	(@cur_rank := @cur_rank + 1) AS ranking
	 
	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
		union all 
		select 1 as score 
	)s,(SELECT @cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

2、跳跃并列排名

## mysql8
SELECT
	score,
	ranking 
FROM
	(
	SELECT
		s.score,
		RANK() OVER(ORDER BY score DESC)as ranking
	FROM
		(
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			1 AS score UNION ALL
		SELECT
			2 AS score UNION ALL
		SELECT
			1 AS score 
			) s
	) a;

##mysql 5.7
select score,ranking
from (
	SELECT 
	 s.score,
	@cur_count := @cur_count + 1,
 if(@pre_score = s.score,@cur_rank,@cur_rank := @cur_count) ranking,
 @pre_score := s.score
	FROM (

		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
		union all 
		select 1 as score 

	)s,(SELECT @cur_count := 0,@cur_rank:=0,@pre_score := NULL)  r 
	ORDER BY s.score desc
) a 
;

3、连续并列排名

##mysql 8
SELECT
	score,
	ranking 
FROM
	(
	SELECT
		s.score,
DENSE_RANK() OVER(ORDER BY score DESC)as ranking
	FROM
		(
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			0 AS score UNION ALL
		SELECT
			1 AS score UNION ALL
		SELECT
			2 AS score UNION ALL
		SELECT
			1 AS score 
			) s
	) a;

##mysql5.7
## if  实现连续并列排名
select score,ranking
from (
	SELECT 
	 s.score,
	 IF(@pre_score = score, @cur_rank , @cur_rank :=@cur_rank +1 ) AS ranking,
	@pre_score := score

	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
	)s,(SELECT @pre_score := null ,@cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

## 连续并列排名  case ...when ... then  遇到零会出现空排名
select score,ranking
from (
	SELECT 
	 s.score,
	 CASE
		WHEN @pre_score = score THEN  @cur_rank
		WHEN @pre_score := score THEN @cur_rank :=@cur_rank +1
	END AS ranking
	 
	FROM (
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 0 as score 
		union all
		select 1 as score
		union all
		select 2 as score
	)s,(SELECT @pre_score := null ,@cur_rank := 0)  r 
	ORDER BY s.score desc
) a 
;

2、分组排名

1、分组连续排名

##mysql 8
select 
cid,score,ranking
from (
	SELECT 
	 s.score,
	 s.cid,
	ROW_NUMBER() OVER (PARTITION BY s.cid ORDER BY s.score DESC) ranking
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s
) a 
;


##mysql5.7
select 
cid,score,ranking
from (
	SELECT 
	 	s.score,
		s.cid,
	 	IF(@pre_cid = cid, @cur_rank :=@cur_rank +1,@cur_rank :=1) AS ranking,
		@pre_cid := cid
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s,(SELECT @pre_cid := null ,@cur_rank := 0)  r 
	ORDER BY s.cid,s.score desc
) a 
;

2、分组并列跳跃排名

##mysql 8
select 
cid,score,ranking
from (
	SELECT 
	 	s.score,
		s.cid,
		RANK() OVER(PARTITION BY s.cid ORDER BY s.score DESC) ranking
	 
	FROM (
		select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid
	)s
) a 
;


##mysql5.7

select 
cid,score,ranking
from (
SELECT s.cid, s.score,
IF(@pre_cid = s.cid,
   @rank_counter := @rank_counter + 1,
   @rank_counter := 1) temp1,
IF(@pre_cid = s.cid,
   IF(@pre_score = s.score, @cur_rank, @cur_rank := @rank_counter),
   @cur_rank := 1) ranking,
@pre_score := s.score temp2,
@pre_cid := s.cid temp3
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s, (SELECT @cur_rank := 0, @pre_cid := NULL, @pre_score := NULL, @rank_counter := 1)r
ORDER BY s.cid, s.score DESC
) a 
;

3、分组并列连续排名

##mysql8
select 
cid,score,ranking
from (
SELECT s.cid, s.score,
DENSE_RANK() OVER(PARTITION BY s.cid ORDER BY s.score DESC)ranking
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s
) a 
;



##mysql.7
select 
cid,score,ranking
from (
SELECT s.cid, s.score,
IF(@pre_score = s.score, @cur_rank, @cur_rank := @cur_rank + 1) temp1,
@pre_score := s.score temp2,
IF(@pre_cid = s.cid, @cur_rank, @cur_rank := 1) ranking,
@pre_cid := s.cid temp3
FROM (	select 0 as score , '第一组' as cid
		union all
		select 0 as score , '第一组' as cid
		union all
		select 0 as score  , '第二组' as cid
		union all
		select 1 as score , '第二组' as cid
		union all
		select 2 as score , '第一组' as cid) s, (SELECT @cur_rank :=0, @pre_score = NULL, @pre_cid := NULL) r
ORDER BY cid, score DESC
) a 
;

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:56:15  更:2022-04-28 11:58:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 1:03:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码