tags:

views:

56

answers:

2

Hi,

I have a table with the following schema:

+------------------+
|Field  | Type     |
+------------------+
| id    | char(30) |
| time  | datetime |
+------------------+

The id column is NOT a primary key, so therefore, the table contains entries such as:

10, 2010-05-05 20:01:01
10, 2010-05-05 20:01:05
13, 2010-05-05 20:04:01
10, 2010-05-05 20:04:10
13, 2010-05-05 20:05:03
10, 2010-05-05 20:05:28

I want to find the difference between times for a given id, i.e. the desired results would be:

10, 00:04:27
13, 00:01:02

I can do this using the following query in MySQL:

SELECT t2.id, TIMEDIFF(t2.max, t2.min) AS visit_length FROM
(
  SELECT id, MAX(time) AS max, MIN(time) AS min FROM page_view 
  GROUP BY id
) AS t2

However, the engine I'm using is a MySQL derivative that doesn't support subqueries.

Is there any way of achieving the same results with a single query without using subqueries?

Thanks

A: 
select max(time)-min(time), id
from page_view
group by id
zed_0xff
thanks, that works, but returns the time in microseconds.
Michael Jones
you can use TIMEDIFF here too: select TIMEDIFF(max(time),min(time)) ...
zed_0xff
A: 

Will this work?

SELECT TIMEDIFF(MAX(time), MIN(TIME)) AS visit_length
FROM page_view
GROUP BY(id)
Jamie Wong
Yes that works perfectly, thanks.
Michael Jones