select
(unix_timestamp(stoptimestamp)-unix_timestamp(starttimestamp))/60.0 diff_minutes
from your_table
使用UNIX_TIMESTAMP()計算以秒爲單位的差異然後通過取決於你在想結果的精度60或60.0分。
要計算多個行的平均下載量,您需要使用SUM()來聚合字節,並計算時間單位。 你可能想使用秒爲inital計算,然後由60.0
下面的例子是用於SQL Sever的書面劃分,因爲我沒有黑斑羚使用
declare @mytable table
([login_id] varchar(11), [starttimestamp_] datetime, [stoptimestamp_] datetime, [download_bytes] decimal(12,1))
;
INSERT INTO @mytable
([login_id], [starttimestamp_], [stoptimestamp_], [download_bytes])
VALUES
('[email protected]', '2015-12-31 23:59:50', '2016-01-01 00:00:20', 438.0),
('[email protected]', '2016-01-01 00:00:28', '2016-01-01 00:01:13', 2190.0),
('[email protected]', '2016-01-01 00:01:21', '2016-01-01 00:01:54', 876.0),
('[email protected]', '2016-01-01 00:01:59', '2016-01-01 00:02:34', 1168.0),
('[email protected]', '2016-01-01 00:02:43', '2016-01-01 00:03:34', 1179.0)
;
select
sum(download_bytes) sum_bytes
, sum(datediff(second,starttimestamp_,stoptimestamp_)) sum_time_unit
, sum(download_bytes)/sum(datediff(second,starttimestamp_,stoptimestamp_)) avg_bytes_sec
, (sum(download_bytes)/sum(datediff(second,starttimestamp_,stoptimestamp_)))/60.0 avg_bytes_min
from @mytable
-- WHERE ...
-- GROUP BY ...
+===========+===============+===============+===============+
| sum_bytes | sum_time_unit | avg_bytes_sec | avg_bytes_min |
+===========+===============+===============+===============+
| 5851 | 194 | 30.159793 | 0.502663 |
+-----------+---------------+---------------+---------------+
見:http://data.stackexchange.com/stackoverflow/query/576857/sql-to-calculate-average-download-in-minutes
download_bytes怎麼樣?我需要平均他們嗎? – akunyer
增加了更多計算來回答 –