SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table test(date date, balance int); insert into test(date, balance) values('2023-01-01', 300), ('2023-01-02', -200), ('2023-01-03', -60), ('2023-01-04', 50), ('2023-01-05', -100), ('2023-01-01', -200), ('2023-01-05', 200), ('2023-01-06', -100), ('2023-01-07', -100), ('2023-01-08', -100); select date, date_add(test.date, interval 1 day) as next_date, rownum() as r from test group by date having sum(balance)<0; WITH recursive date_ranges AS ( (select date, date_add(test.date, interval 1 day) as next_date, rownum() as r from test group by date having sum(balance)<0) union all select t.*, date_add(t.date, interval 1 day) as next_date, date_ranges.r from date_ranges join (select date from test group by test.date having sum(balance)<0) as t where t.date = date_ranges.next_date ) select count(*) from date_ranges group by r order by count(*) desc limit 1 ;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear