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 t (effective_date date, security varchar(50), market_price int); insert into t select '20211201', 'STOCKNAME',99 ; insert into t select '20211202', 'STOCKNAME',98 ; insert into t select '20211203', 'STOCKNAME',97 ; insert into t select '20211206', 'STOCKNAME',101; insert into t select '20211207', 'STOCKNAME',99 ; insert into t select '20211208', 'STOCKNAME',98 ; insert into t select '20211209', 'STOCKNAME',97 ; insert into t select '20211210', 'STOCKNAME',96 ; insert into t select '20211213', 'STOCKNAME',102; insert into t select '20211214', 'STOCKNAME',99 ; insert into t select '20211215', 'STOCKNAME',98 ; insert into t select '20211216', 'STOCKNAME',97 ; insert into t select '20211217', 'STOCKNAME',96 ; insert into t select '20211220', 'STOCKNAME',95 ; insert into t select '20211221', 'STOCKNAME',99 ; insert into t select '20211222', 'STOCKNAME',102; insert into t select '20211223', 'STOCKNAME',103; insert into t select '20211224', 'STOCKNAME',114;
with g as ( select *, Row_Number() over (partition by security order by effective_date) - Row_Number() over (partition by security, case when market_price<100 then 1 else 0 end order by effective_date) cnt from t ) select security, Count(*) number_of_days from g group by security, cnt order by number_of_days desc limit 1
security
number_of_days
STOCKNAME
6