示例数据:
╔════╦════════════╦════════════╦═══════╦═══════════╦════════╗ ║ ID ║ START ║ STOP ║ USER ║ FILE ║ SIZE ║ ╠════╬════════════╬════════════╬═══════╬═══════════╬════════╣ ║ 1 ║ 1330133409 ║ 1330133410 ║ user1 ║ file1.zip ║ 300000 ║ ║ 2 ║ 1330133409 ║ 1330133410 ║ user1 ║ file2.zip ║ 300500 ║ ║ 3 ║ 1330133409 ║ 1330133410 ║ user2 ║ file1.zip ║ 300000 ║ ║ 4 ║ 1330133409 ║ 1330133410 ║ user2 ║ file2.zip ║ 300500 ║ ║ 5 ║ 1330133409 ║ 1330133410 ║ user1 ║ file3.zip ║ 500000 ║ ║ 6 ║ 1330133409 ║ 1330133310 ║ user6 ║ file3.zip ║ 500000 ║ ╚════╩════════════╩════════════╩═══════╩═══════════╩════════╝
我需要创建一个MySQL查询来计算PER_USER_AVERAGE_BANDWIDTH
PER_USER_AVERAGE_BANDWIDTH = SUM(SIZE) / (STOP - START)
,然后按PER_USER_AVERAGE_BANDWIDTH
排序以生成如下结果:
╔═══════╦════════════════════════════╗ ║ USER ║ PER_USER_AVERAGE_BANDWIDTH ║ ╠═══════╬════════════════════════════╣ ║ user3 ║ 110.37 ║ ║ user1 ║ 100.25 ║ ║ user2 ║ 75.70 ║ ╚═══════╩════════════════════════════╝
清除泥土;)任何人?
答案 0 :(得分:2)
我认为您的平均值应该是总持续时间的总大小,按用户分组:
SELECT USER,
SUM(SIZE) / SUM(STOP - START) AS PER_USER_AVERAGE_BANDWIDTH
FROM my_table
GROUP BY USER
ORDER BY PER_USER_AVERAGE_BANDWIDTH DESC
在sqlfiddle上查看。
答案 1 :(得分:2)
直接进行平均
SELECT
`user`,
AVG( size / ( stop - start ) ) per_user_average_bandwidth
FROM
tab_dl
GROUP BY `user`
ORDER BY per_user_average_bandwidth DESC
答案 2 :(得分:0)
此查询应该这样做:
SELECT USER, (SUM(SIZE) / (STOP - START)) AS PER_USER_AVERAGE_BANDWIDTH
FROM table
GROUP BY USER, stop, start
ORDER BY PER_USER_AVERAGE_BANDWIDTH DESC
这将为您提供每个唯一时间范围内每位用户的平均带宽(即,如果用户在时间1和时间5之间下载文件1和文件2,则在时间1和时间10之间为文件3提供2行)