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
SQL Server
SQLite
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
2012
2014
2016
2017
2017 (Linux)
2019
2019 (Linux)
2022
3.8
3.16
3.27
3.39
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
AdventureWorks
no sample DB
no sample DB
AdventureWorks
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
run
markdown
donate
feedback
about
By using db<>fiddle, you agree to license everything you submit by
Creative Commons CC0
.
CREATE TABLE Data ( `order` varchar(10) NOT NULL, sn varchar(10) NOT NULL, ag varchar(10) NOT NULL, af varchar(10) NOT NULL )
INSERT INTO Data (`order`, sn, ag, af) VALUES ('a','0001','123','0'), ('a','0001','124','0'), ('a','0001','125','0'), ('a','0001','126','0'), ('a','0003','123','1'), ('a','0003','123','2'), ('a','0003','124','0'), ('a','0003','125','0'), ('a','0003','126','0'), ('b','0005','123','0'), ('b','0005','124','0'), ('b','0006','123','0'), ('b','0006','124','1'), ('b','0006','124','2'), ('b','0006','125','0')
Records: 15 Duplicates: 0 Warnings: 0
SELECT `order`, SUM(bb) AS bb, SUM(ff) AS ff FROM ( SELECT `order`, sn, CASE WHEN rule1>0 THEN 1 ELSE 0 END AS bb, CASE WHEN rule2>=1 AND rule3=0 THEN 1 ELSE 0 END AS ff FROM ( SELECT `order`, sn, SUM(CASE WHEN af<>'0' THEN 1 ELSE 0 END) AS rule1, SUM(CASE WHEN ag='126' AND af='0' THEN 1 ELSE 0 END) AS rule2, SUM(CASE WHEN ag IN('123','124','125') AND af<>'0' THEN 1 ELSE 0 END) AS rule3 FROM Data GROUP BY `order`, sn ) AS A ) AS B GROUP BY `order` ORDER BY `order`
order
bb
ff
a
1
1
b
1
0