SQL 以分钟为单位计算平均下载量
SQL to calculate average download in minutes
我无法从以下 ANSI-92 SQL 或 Impala SQL标准。
- login_id ,开始时间戳 ,停止时间戳 ,download_bytes
- abc@fcc.com, 2015-12-31 23:59:50, 2016-01-01 00:00:20, 438.0
- abc@fcc.com, 2016-01-01 00:00:28, 2016-01-01 00:01:13, 2190.0
- abc@fcc.com, 2016-01-01 00:01:21, 2016-01-01 00:01:54, 876.0
- abc@fcc.com, 2016-01-01 00:01:59, 2016-01-01 00:02:34, 1168.0
- abc@fcc.com, 2016-01-01 00:02:43, 2016-01-01 00:03:34, 1179.0
粗体显示的时间共享starttimestamp 和stoptimestamp 的时间space(以分钟为单位)。如何获得
中的平均下载量
- 00:00:00 分钟 ( 00:00:20 - 00:00:28 )
- 00:01:00 分钟 ( 00:01:13 - 00:01:21 )
- 00:02:00 分钟 ( 00:02:34 - 00:02:43 )
等等。
有什么建议吗?非常感谢您!
此致,
波兹
select
(unix_timestamp(stoptimestamp)-unix_timestamp(starttimestamp)) / 60.0 diff_minutes
from your_table
使用 unix_timestamp() 计算以秒为单位的差异,然后根据您希望结果的精度除以 60 或 60.0。
要计算多行的平均下载量,您需要使用 SUM() 来聚合字节并计算时间单位。 您可能希望使用秒数进行初始计算,然后除以 60.0
以下示例是为 SQL 服务器编写的,因为我没有 Impala 可以使用
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
('abc@fcc.com', '2015-12-31 23:59:50', '2016-01-01 00:00:20', 438.0),
('abc@fcc.com', '2016-01-01 00:00:28', '2016-01-01 00:01:13', 2190.0),
('abc@fcc.com', '2016-01-01 00:01:21', '2016-01-01 00:01:54', 876.0),
('abc@fcc.com', '2016-01-01 00:01:59', '2016-01-01 00:02:34', 1168.0),
('abc@fcc.com', '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 |
+-----------+---------------+---------------+---------------+
参见:https://data.stackexchange.com/Whosebug/query/576857/sql-to-calculate-average-download-in-minutes
我无法从以下 ANSI-92 SQL 或 Impala SQL标准。
- login_id ,开始时间戳 ,停止时间戳 ,download_bytes
- abc@fcc.com, 2015-12-31 23:59:50, 2016-01-01 00:00:20, 438.0
- abc@fcc.com, 2016-01-01 00:00:28, 2016-01-01 00:01:13, 2190.0
- abc@fcc.com, 2016-01-01 00:01:21, 2016-01-01 00:01:54, 876.0
- abc@fcc.com, 2016-01-01 00:01:59, 2016-01-01 00:02:34, 1168.0
- abc@fcc.com, 2016-01-01 00:02:43, 2016-01-01 00:03:34, 1179.0
粗体显示的时间共享starttimestamp 和stoptimestamp 的时间space(以分钟为单位)。如何获得
中的平均下载量- 00:00:00 分钟 ( 00:00:20 - 00:00:28 )
- 00:01:00 分钟 ( 00:01:13 - 00:01:21 )
- 00:02:00 分钟 ( 00:02:34 - 00:02:43 )
等等。
有什么建议吗?非常感谢您!
此致,
波兹
select
(unix_timestamp(stoptimestamp)-unix_timestamp(starttimestamp)) / 60.0 diff_minutes
from your_table
使用 unix_timestamp() 计算以秒为单位的差异,然后根据您希望结果的精度除以 60 或 60.0。
要计算多行的平均下载量,您需要使用 SUM() 来聚合字节并计算时间单位。 您可能希望使用秒数进行初始计算,然后除以 60.0
以下示例是为 SQL 服务器编写的,因为我没有 Impala 可以使用
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
('abc@fcc.com', '2015-12-31 23:59:50', '2016-01-01 00:00:20', 438.0),
('abc@fcc.com', '2016-01-01 00:00:28', '2016-01-01 00:01:13', 2190.0),
('abc@fcc.com', '2016-01-01 00:01:21', '2016-01-01 00:01:54', 876.0),
('abc@fcc.com', '2016-01-01 00:01:59', '2016-01-01 00:02:34', 1168.0),
('abc@fcc.com', '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 |
+-----------+---------------+---------------+---------------+
参见:https://data.stackexchange.com/Whosebug/query/576857/sql-to-calculate-average-download-in-minutes