Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
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); WITH cte AS ( SELECT SUM(CASE WHEN balance < 0 THEN 0 ELSE 1 END) OVER (ORDER BY date) grp FROM test ) SELECT COUNT(*) - 1 max_cnt FROM cte GROUP BY grp ORDER BY 1 DESC LIMIT 1;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear