I need help with a SQL query.
I have a table with a ‘state’ column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of ‘state’ = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here’s you could probably do it :
If
is_consecutive = 1, then, yes, there is 5 last consecutive rows withstate = 1.Edit : As suggested in the comments, you’ll have to use
ORDER BYin your query, to get the last nth rows.And for more accuracy, since you have a timestamp column, you should use
Status_datetimeto order the rows.