sql以分钟计算平均下载量

nfeuvbwi  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(315)

在ansi-92sql或impalasql标准中,我很难从示例记录中获得几分钟内的平均下载量。
登录\u id,开始时间戳,停止时间戳,下载\u字节
@fcc.com, 2015-12-31 23:59:50, 2016-01-01 00:00:20, 438.0
@fcc.com, 2016-01-01 00:00:28, 2016-01-01 00:01:13, 2190.0
@fcc.com, 2016-01-01 00:01:21, 2016-01-01 00:01:54, 876.0
@fcc.com, 2016-01-01 00:01:59, 2016-01-01 00:02:34, 1168.0
@fcc.com, 2016-01-01 00:02:43, 2016-01-01 00:03:34, 1179.0
以粗体显示的时间共享starttimestamp和stoptimestamp的时间空间(以分钟组为单位)。如何获得平均下载量
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)
等等。
有什么建议吗?事先非常感谢!
当做,
波齐

wqnecbli

wqnecbli1#

select
    (unix_timestamp(stoptimestamp)-unix_timestamp(starttimestamp)) / 60.0 diff_minutes
from your_table

使用unix\u timestamp()计算以秒为单位的差值,然后根据结果的精度除以60或60.0。
要计算几行的平均下载量,需要使用sum()来聚合字节并计算时间单位。您可能希望使用秒进行初始计算,然后除以60.0
下面的例子是为sqlsever编写的,因为我没有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      |
+-----------+---------------+---------------+---------------+

请参见:http://data.stackexchange.com/stackoverflow/query/576857/sql-to-calculate-average-download-in-minutes

相关问题