• Jan,

    Thanks, but this is not the result I am after. My table has three count columns (open_front, open_rear, wrap_count) plus a timestamp of when the record was added (t_stamp). Each count column is a counter value in a PLC (so 3 independant counters). The PLC counters resets to zero at 32000 (any counter can reset to zero independant of the other two counters - by the time a new row is INSERTED the counter value could already have incremented away from zero so the rows could go 31998, 2, 3, 8 etc) but there is no recording of this happening so it has to be 'detected' in the query by the previous row being higher than the row beneath it (i.e. 31999 followed by 1 = a rollover).

    (see drawing attached - I know drawings are not the usual thing but I'm hoping it helps).

    My results need to be a count (as described above and in drawing) for open_front, open_rear, wrap_count grouped by day:

    Open Front..........Open Rear........Wrap_Count........Date

    2.......................100.................30000................1/1/2013

    32006.................0....................39000................2/1/2013

    From the test data open_front for the 2/1/2013 resulted from:

    Initial value :2 (therefore 32000 - 2) = 31998

    Rollover count:1 (therefore 1-1*32000) = 0

    End value = 8

    Giving a total for the 2/1/2013 for open_front: 32006

    For wrap_count on the 2/1/2013 we see this:

    Initial value:31000 (therefore 32000 - 31000) = 1000

    Rollover count:2 (therefore 2-1*32000) = 32000

    End value:6000

    Giving a total for 2/1/2013 for wrap_count: 39000

    The test data might not look logical but I was trying to show that 0 and 32000 (top and bottom values) are not always recorded in the SQL due to a slow INSERT of data but a fast PLC count rate but a count had to have reached 32000 before reseting even if the SQL showed:

    31970

    31988

    1

    10

    Thanks