'How do I get "no" calculated with complex calculations and conditions in SQLite3

If I have a table like

no size name
1 30 toys
2 23 shelf
3 50 monitor
4 62 carrier
5 51 books
6 45 electrics

If there is a size limit and you have to delete it from the beginning of a certain "no"

Is there any sqlite3 query to get a certain "no"?

example,

limited size : 210 I should get "no" : 2 ( id 3 + id 4 + id 5 + id 6 = 208 )



Solution 1:[1]

Use a correlated subquery in the WHERE clause of the DELETE statement like this:

DELETE FROM tablename AS t1
WHERE (SELECT SUM(CASE WHEN t2.no >= t1.no THEN t2.size END) FROM tablename t2) > ?; 

Change ? to 210 or any other limit that you want.

See the demo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 forpas