I really don’t know how to put a query for this task.
I need to get a time for the first row of each record in the table but every time when I have a group of same results I need to get the first one when it occurs and when signal is equal to 1.
I don’t know how to explain it correctly but I will make an example and you’ll see what I mean.
Records:
ID| Time | Serial | Signal
1| 10:59| 12 | 1
2| 11:00| 12 | 1
3| 11:01| 12 | 1
4| 11:01| 13 | 1
5| 11:02| 12 | 0
6| 11:03| 12 | 0
7| 11:05| 13 | 1
8| 11:07| 12 | 1
So I need this result:
ID| Time | Serial | Signal
1| 10:59| 12 | 1
4| 11:01| 13 | 1
7| 11:05| 13 | 1
8| 11:07| 12 | 1
Your exact logic is not clear, from what I can gather you are looking to get each row where
signalis 1 where the serial value changes from one time to another.This can be done using a variable to track the value of the serial in the previous row (variable @t in my query below).
SQL Fiddle