Curtis Crewe
Curtis Crewe

Reputation: 4336

Finding averages timestamp based on two rows

Basically i'm attempting to get an average based on two time stamps inside a database, let's say for example i have a product, I insert it into the database 10 times with an listed_time and a sell_time, I want to group all 10 rows together and find the average between both the listed_time and the sell_time to find on average on how long it took to sell.

EDIT : timestamps are inserted as time() not date()

The code in which i tried and failed to work was

$query = $db->runSQL("SELECT AVG(TIMESTAMPDIFF(SECOND, listed_time, sell_time)) from trade");
while($row = mysql_fetch_assoc($query)) {
    echo $row['AVG(TIMESTAMPDIFF(SECOND, listed_time, sell_time))']."</br>";
}

Upvotes: 0

Views: 89

Answers (1)

Stephan
Stephan

Reputation: 8090

You can try this for that 10 rows:

SELECT AVG(TIMESTAMPDIFF(SECOND, listed_time, sell_time ) ) as za_avg ...

This will give you the average time (for those 10 rows) it took to sell the items

Upvotes: 2

Related Questions