mysql - Select last N rows following a condition -


i've got database table logs has 3 columns:

date | status | projectid 

status can either 0 or 1, primary key on date , projectid

i'm trying find out how many times projectid had status 0 since last time 1.

so if there 1 projectid

date | status | projectid   1       0        3   2       0        3   3       1        3   4       1        3   5       0        3   6       0        3 

this should return 2 (row 5 , 6 0 , row 4 1)

the thing makes hard me have maintain order of date. way tackle such problems, , 1 in particular?

here how 1 project:

select count(*) logs l status = 0 ,       projectid = 3 ,       date > (select max(date) logs projectid = 3 , status = 1) 

here how projects:

select l.projectid, count(l1.projectid) logs l left outer join      (select projectid, max(date) maxdate       logs       status = 1       group projectid      ) l1      on l.projectid = l1.projectid ,         l.date > l1.date ,         l.status = 0 group l.projectid; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -