tags:

views:

144

answers:

6

There is a table with Columns as below:

Id : long autoincrement; 
timestamp:long; 
price:long

Timestamp is given as a unix_time in ms.

Question: what is the average time difference between the records ?

A: 

At a guess:

SELECT AVG(timestamp)

I think you need to provide more information in your question for us to help.

Gav
assume there are n records. question is like average time difference between the records?here, tricky is difference between records. cam you tell me what kind of information do you require more ?
Siv
+1  A: 

If you mean difference between each-other row:

select AVG(x) from (
select a.timestamp - b.timestamp as x 
from table a, table b -- this multiplies a*b ) sub
Dewfy
i have only one table and multiple records. need between records the time diff. not with multiple tables.
Siv
well, but this is going to associate each entry with each other entry, e.g. you get entry1 - enty1, entry1-entry2, entry1-entry3 and so on - the average will not be just between entries that are "neighbors" to one another......
marc_s
@Siv - this query uses exactly 1 table - but select from it twice (using alias a, b)
Dewfy
A: 
SELECT AVG(T2.Timestamp - T1.TimeStamp)
  FROM Table T1
  JOIN Table T2 ON T2.ID = T1.ID + 1
n8wrl
My assumption was (and you know where that gets you) was the OP was interested in average difference between consecutive records. T2.ID cannot be 'beyond the range of valid ID's' because there's no record there!
n8wrl
Any gaps in the Id column would cause a break.
Adam Ruth
@Adam: Oops - you're right!
n8wrl
+1  A: 

In SQL Server, you could write something like that to get that information:

SELECT 
  t1.ID, t2.ID,
  DATEDIFF(MILLISECOND, t2.PriceTime, test2.PriceTime)
FROM table t1
INNER JOIN table t2 ON t2.ID = t1.ID-1
WHERE t1.ID > (SELECT MIN(ID) FROM table)

and if you're only interested in the AVG across all entries, you could use:

SELECT 
  AVG(DATEDIFF(MILLISECOND, t2.PriceTime, test2.PriceTime))
FROM table t1
INNER JOIN table t2 ON t2.ID = t1.ID-1
WHERE t1.ID > (SELECT MIN(ID) FROM table)

Basically, you need to join the table with itself, and use "t1.ID = t2.ID-1" to associate item no. 2 in one table with item no. 1 in the other table and then calculate the time difference between the two. In order to avoid accessing item no. 0 which doesn't exist, use the "T1.ID > (SELECT MIN(ID) FROM table)" clause to start from the second item.

Marc

marc_s
This will only work if there are no gaps in Id.
Adam Ruth
@Adam: true - good point
marc_s
OK, so it means it does indeed work - see OP's comment - since it's an "AUTOINCREMENT" column, chances are it'll be without gaps (although not guaranteed to be that way :-) )
marc_s
That's good, yours would be a ton faster than mine!
Adam Ruth
+2  A: 

First thought is a sub-query grabbing the record immediately previous:

SELECT timestamp - 
(select top 1 timestamp from Table T1 where T1.Id < Table.Id order by Id desc)
FROM Table

Then you can take the average of that:

SELECT AVG(delta)
from (SELECT timestamp - 
    (select top 1 timestamp from Table T1 where T1.Id < Table.Id order by Id desc) as delta
    FROM Table) T

There will probably need to be some handling of the null that results for the first row, but I haven't tested to be sure.

Adam Ruth
You'll need to adjust the "top 1" part to the Oracle equivalent, it's been too long since I've worked with Oracle.
Adam Ruth
A: 

try this

   Select Avg(E.Timestamp - B.Timestamp)
   From Table B Join Table E 
      On E.Timestamp = 
           (Select Max(Timestamp)
            From Table
            Where Timestamp < R.Timestamp)
Charles Bretana