Efficient SQL to count an occurrence in the latest X rows

Posted by pulegium on Stack Overflow See other posts from Stack Overflow or by pulegium
Published on 2010-04-20T20:36:26Z Indexed on 2010/04/20 20:43 UTC
Read the original article Hit count: 186

Filed under:
|
|

For example I have:

create table a (i int);

Assume there are 10k rows.

I want to count 0's in the last 20 rows.

Something like:

select count(*) from (select i from a limit 20) where i = 0;

Is that possible to make it more efficient? Like a single SQL statement or something?

PS. DB is SQLite3 if that matters at all...

© Stack Overflow or respective owner

Related posts about sql

Related posts about optimization