add batch
remove batch
split batch
comment selection
show hidden batches
hide batch
highlight batch
db<>fiddle
Db2
Firebird
MariaDB
MySQL
Node.js
Oracle
Postgres
SQLite
SQL Server
TimescaleDB
YugabyteDB
Developer-C 11.1
3.0
4.0
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
5.5
5.6
5.7
8.0
18
11g Release 2
18c
21c
23c
8.4
9.3
9.4
9.5
9.6
10
11
12
13
14
15
16
3.8
3.16
3.27
3.39
2012
2014
2016
2017
2017 (Linux)
2019
2019 (Linux)
2022
2.11
2.14
2.6
2.8
2.18
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
Sakila
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
AdventureWorks
no sample DB
no sample DB
AdventureWorks
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
run
markdown
donate
feedback
about
By using db<>fiddle, you agree to license everything you submit by
Creative Commons CC0
.
CREATE TABLE products ( id int NOT NULL AUTO_INCREMENT PRIMARY KEY, category varchar(255) NOT NULL ); INSERT INTO products(category) VALUES ('A'), ('B'), ('B'), ('C'), ('A'), ('D'), ('C'), ('D'), ('D'), ('C'), ('C'), ('E'), ('E'), ('A'), ('F'), ('D'), ('D'), ('G'), ('H'), ('G'), ('H'), ('G');
select case when rn <= 3 then real_category else 'Other' end final_category, sum(no_products) no_products from ( select coalesce(x.new_category, p.category) real_category, count(*) no_products, rank() over(order by count(*) desc) rn from products p left join ( select 'A' category, 'A-G' new_category union all select 'G', 'A-G' union all select 'B', 'B-C' union all select 'C', 'B-C' union all select 'E', 'E-F' union all select 'F', 'E-F' ) x on x.category = p.category group by real_category ) t group by final_category order by no_products desc
final_category
no_products
A-G
6
B-C
6
D
5
Other
5