We have a database that we are using to store test results for an embedded device. There's a table with columns for different types of failures (details not relevant), along with a primary key 'keynum' and a 'NUM\_FAILURES' column that lists the number of failures. We store passes and failures, so a pass has a '0' in 'NUM\_FAILURES'.

In order to keep the database from growing without bounds, we want to keep the last 1000 results, plus any of the last 50 failures that fall outside of the 1000. So, worst case, the table could have 1050 entries in it. I'm trying to find the most efficient SQL insert trigger to remove extra entries. I'll give what I have so far as an answer, but I'm looking to see if anyone can come up with something better, since SQL isn't something I do very often.

We are using SQLITE3 on a non-Windows platform, if it's relevant.

**EDIT:** To clarify, the part that I am having problems with is the DELETE, and specifically the part related to the last 50 failures.