1

I need to be able to find first and the second record with sql only if time between those 2 is 15 or more minutes.

Let me put a little example so you can see what I need:

ID| Time | Serial |   
 1| 10:45|   13   |  
 2| 11:00|   12   |  
 3| 11:17|   12   |  
 4| 11:00|   13   |  
 7| 11:05|   13   |  
 8| 11:07|   12   |  

I need to get this lines:

ID| Time | Serial |

1| 10:45| 13 |
2| 11:00| 12 |
3| 11:17| 12 |
4| 11:00| 13 |
7| 11:05| 13 |
8| 11:07| 12 |

And output like this:

Time 1 | Time 2 | Serial | Usage  
 10:45 | 11:00  | 13     | 15min
 11:00 | 11:17  | 12     | 17min

EDIT: I only need to compare 2 records at the same time with the same serial number. And of course go trough all the records. I have this type of the data in time column:2012-09-06 16:53:05.581

4

1 に答える 1

1

Maybe something along these lines:

SELECT
    time1.time,
    time2.time,
    time1.serial,
    (time2.time - time1.time) / 60 AS usage
FROM
    mytable time1,
    mytable time2
WHERE
    time1.id != time2.id
    AND time1.serial = time2.serial
    AND time2.time - time1.time > 60 * 15

I am not familiar with time handling in MySQL - I am assuming that they can be subtracted, and that the difference is in seconds. Use a function here if it requires explicit conversion.

Addendum: this should handle pairs of record entries fine. However if there are three that each have 15 minutes between them, then this will produce 1-2, 2-3 and 1-3, so you may need to modify it a bit.

于 2012-09-26T18:58:26.923 に答える