SQL 难点解决:直观分组

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1、    对位分组

示例 1:按顺序分别列出使用 Chinese、English、French 作为官方语言的国家数量

MySQL8:

with t(name,ord) as (select 'Chinese',1

union all select 'English',2

union all select 'French',3)

select t.name, count(countrycode) cnt

from t left join world.countrylanguage s on t.name=s.language

where s.isofficial='T'

group by name,ord

order by ord;

注意:表的字符集和数据库会话的字符集要保持一致。

(1)   show variables like 'character_set_connection'查看当前会话字符集

(2)   show create table world.countrylanguage查看表的字符集

(3)   set character_set_connection=[字符集]更新当前会话字符集

 

集算器SPL:
1
A1: 连接数据库

A2: 查询出所有官方语言的记录

A3: 需要列出的语言

A4: 将所有记录按Language对位到A3相应位置

A5: 构造以语言和使用此语言为官方语言的国家数量的序表
2

示例 2:按顺序分别列出使用 Chinese、English、French 及其它语言作为官方语言的国家数量

MySQL8:

with t(name,ord) as (select 'Chinese',1 union all select 'English',2

union all select 'French',3 union all select 'Other', 4),

s(name, cnt) as (

select language, count(countrycode) cnt

from world.countrylanguage s

where s.isofficial='T' and language in ('Chinese','English','French')

group by language

union all

select 'Other', count(distinct countrycode) cnt

from world.countrylanguage s

where isofficial='T' and language not in ('Chinese','English','French')

)

select t.name, s.cnt

from t left join s using (name)

order by t.ord;

 

集算器SPL:
3
A4: 将所有记录按Language对位到A3.to(3)相应位置,并追加一组用于存放不能对位的记录

A5: 第4组计算不同CountryCode的数量
4

2、    枚举分组

示例 1:按顺序列出各类型城市的数量

MySQL8:

with t as (select * from world.city where CountryCode='CHN'),

segment(class,start,end) as (select 'tiny', 0, 200000

union all select 'small',  200000, 1000000

union all select 'medium', 1000000, 2000000

union all select 'big', 2000000, 100000000

)

select class, count(1) cnt

from segment s join t on t.population>=s.start and t.population

group by class, start

order by start;

 

集算器SPL:
5
A3: ${…}宏替换,以大括号内表达式的结果作为新表达式进行计算,结果为序列["?<200000","?<1000000","?<2000000","?<100000000"]

A5: 针对 A2 中每条记录,寻找 A3 中第 1 个成立的条件,并追加到对应的组中
6

示例 2:列出华东地区大型城市数量、其它地区大型城市数量、非大型城市数量

MySQL8:

with t as (select * from world.city where CountryCode='CHN')

select 'East&Big' class, count(*) cnt

from t

where population>=2000000

and district in ('Shanghai','Jiangshu', 'Shandong','Zhejiang','Anhui','Jiangxi')

union all

select 'Other&Big', count(*)

from t

where population>=2000000

and district not in ('Shanghai','Jiangshu','Shandong','Zhejiang','Anhui','Jiangxi')

union all

select 'Not Big', count(*)

from t

where population<2000000;

 

集算器SPL:
7
A5: enum@n将不满足 A4 中所有条件的记录存放到追加的最后一组中
8

示例 3:列出所有地区大型城市数量、华东地区大型城市数量、非大型城市数量

MySQL8:

with t as (select * from world.city where CountryCode='CHN')

select 'Big' class, count(*) cnt

from t

where population>=2000000

union all

select 'East&Big' class, count(*) cnt

from t

where population>=2000000

and district in ('Shanghai','Jiangshu','Shandong','Zhejiang','Anhui','Jiangxi')

union all

select 'Not Big' class, count(*) cnt

from t

where population<2000000;

 

集算器SPL:
9
A6: 若A2中记录满足A4中多个条件时,enum@r会将其追加到对应的每个组中
10

3、 返回值直接作为序号进行定位分组

示例 1: 按顺序列出各类型城市的数量

MySQL8: 参见“枚举分组”中 SQL

集算器SPL:
11
A5: 先计算 A2.Population 在 A3 中段号,然后根据段号进行定位分组

 

4、    原序保持下的相邻记录分组

示例 1: 列出前 10 届奥运金牌榜 (olympic 表中只有历届成绩前 3 名的信息,且没有奖牌完全相同的情况)

MySQL8:

with t1 as (select ,rank() over(partition by game order by gold1000000+silver*1000+copper desc) rn from olympic where game<=10)

select game,nation,gold,silver,copper from t1 where rn=1;

 

集算器SPL:
12
A3: 按原序分到各组,每组取第 1 条记录组成新序表
13

示例 2: 求奥运会国家总成绩蝉联第 1 的最长届数

MySQL8:

with t1 as (select ,rank() over(partition by game order by gold1000000+silver*1000+copper desc) rn from olympic),

t2 as (select game,ifnull(nation<>lag(nation) over(order by game),0)neq from t1 where rn=1),

t3 as (select sum(neq) over(order by game) acc from t2),

t4 as (select count(acc) cnt from t3 group by acc)

select max(cnt) cnt from t4;

t1: 求出成绩排名

t2: 列出历届第1名,并根据nation是否与上届不同置标志neq(不同置1,相同置0)

t3: 累积标志neq到acc,可以保证相邻nation相同的acc相同,不相邻nation的acc不相同

 

集算器SPL:
14
A4: 将相邻nation相同的记录按原序分到同组

A5: 求各组长度的最大值即最大届数
15

示例3:列出奥运会总成绩排名第一最长蝉联时的各届信息

MySQL:

with t1 as (select ,rank() over(partition by game order by gold1000000+silver*1000+copper desc) rn from olympic),

t2 as (select *,ifnull(nation<>lag(nation) over(order by game),0)neq from t1 where rn=1),

t3 as (select *, sum(neq) over(order by game) acc from t2),

t4 as (select acc,count(acc) cnt from t3 group by acc),

t5 as (select * from t4 where cnt=(select max(cnt) cnt from t4))

select game,nation,gold,silver,copper from t3 join t5 using (acc);

 

集算器SPL:
16
A5: 求出长度最大组
17

示例 4:求奥运会前3名金牌总数连续增长的最大届数

MySQL8:

with t1 as (select game,sum(gold) gold from olympic group by game),

t2 as (select game,gold, gold<=lag(gold,1,-1) over(order by game) lt from t1),

t3 as (select game, sum(lt) over(order by game) acc from t2),

t4 as (select count(*) cnt from t3 group by acc)

select max(cnt)-1 cnt from t4;

 

集算器SPL:
18

A3: 根据条件值按原序分组,若gold小于等于上一个gold则产生新分组
19

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
27天前
|
SQL
sql server模糊查询、分组
sql server模糊查询、分组
12 1
|
4月前
|
SQL 大数据 HIVE
每天一道大厂SQL题【Day10】电商分组TopK实战
每天一道大厂SQL题【Day10】电商分组TopK实战
31 0
|
3月前
SQL-分组查询
SQL-分组查询
|
3月前
|
SQL 自然语言处理
leecode-SQL-1484-按日期分组销售产品
leecode-SQL-1484-按日期分组销售产品
27 0
|
3月前
|
SQL
四、SQL分组查询
四、SQL分组查询
31 0
|
3月前
|
SQL 自然语言处理 数据挖掘
「SQL面试题库」 No_115 按日期分组销售产品
「SQL面试题库」 No_115 按日期分组销售产品
|
4月前
|
关系型数据库 MySQL 数据库
Mysql数据库 6.SQL语言 分组、分页查询
Mysql数据库 6.SQL语言 分组、分页查询
59 2
|
5月前
|
SQL 关系型数据库 MySQL
sql处理重复的列,更好理清分组和分区
sql处理重复的列,更好理清分组和分区
45 0
|
6月前
|
SQL 关系型数据库 Serverless
深入理解 SQL 分组查询
SQL 是结构化查询语言(Structured Query Language)的缩写,是用于管理关系型数据库的标准语言。在 SQL 中,查询是其中最重要的部分之一,通过查询,我们可以从数据库中检索所需的数据。分组查询是 SQL 查询中的一项重要功能,它允许我们对数据进行分组、聚合和汇总,以便更好地理解数据的特征和趋势。 在本文中,我们将深入探讨 SQL 中的分组查询,包括其基本语法、常用聚合函数、分组筛选条件、多重分组、分组排序等方面的内容。无论您是初学者还是有一定 SQL 基础的开发者,都将从本文中获得有关 SQL 分组查询的详细信息。
364 0
|
8月前
|
SQL 数据库
数据库——SQL通过某字段的取值范围进行分组汇总
数据库——SQL通过某字段的取值范围进行分组汇总