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); 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 ChatGPT!
Copy Clear