mysql - SQL:找出最大的响应时间变化百分比

标签 mysql sql statistics

我正在尝试按照从一个时间段到下一个时间段的最大响应时间整体减少的顺序来计算和列出网站。

我严格不需要使用单个查询来执行此操作,我可以潜在地运行多个查询。

网站:

| id | url                    |
| 1  | stackoverflow.com      |
| 2  | serverfault.com        |
| 3  | stackexchange.com      |


回应:

| id | website_id | response_time | created_at |
| 1  | 1          | 93.26         | 2014-01-28 11:51:39
| 2  | 1          | 99.46         | 2014-01-28 11:52:38
| 2  | 1          | 94.51         | 2014-01-28 11:53:38
| 2  | 1          | 104.46        | 2014-01-28 11:54:38
| 2  | 1          | 85.46         | 2014-01-28 11:56:38
| 2  | 1          | 100.00        | 2014-01-28 11:57:36
| 2  | 1          | 50.00         | 2014-01-28 11:58:37
| 2  | 2          | 100.00        | 2014-01-28 11:58:38
| 2  | 2          | 80            | 2014-01-28 11:58:39


理想情况下,结果应如下所示:

| percentage_change | website_id | 
| 52                | 1 | 
| 20                | 2 | 


我已经弄清楚了最大的响应时间,但是不知道如何执行另一个查询来计算最小的响应时间,然后进行数学运算,然后对数学进行排序。

SELECT * FROM websites
LEFT JOIN (
SELECT distinct * 
       FROM responses
       ORDER BY response_time desc) responsetable
ON websites.id=responsetable.website_id group by website_id


谢谢

最佳答案

使用几个序列号:-

SELECT a.id, a.url, MAX(100 * (LeadingResponse.response_time - TrailingResponse.response_time) / LeadingResponse.response_time)
FROM
(
    SELECT website_id, created_at, response_time, @aCnt1 := @aCnt1 + 1 AS SeqCnt
    FROM responses
    CROSS JOIN
    (
        SELECT @aCnt1:=1
    ) Deriv1
    ORDER BY website_id, created_at
) TrailingResponse
INNER JOIN
(
    SELECT website_id, created_at, response_time, @aCnt2 := @aCnt2 + 1 AS SeqCnt
    FROM responses
    CROSS JOIN
    (
        SELECT @aCnt2:=2
    ) Deriv2
    ORDER BY website_id, created_at
) LeadingResponse
ON LeadingResponse.SeqCnt = TrailingResponse.SeqCnt
AND LeadingResponse.website_id = TrailingResponse.website_id
INNER JOIN websites a
ON LeadingResponse.website_id = a.id
GROUP BY a.id, a.url


SQL为此提琴:-

http://www.sqlfiddle.com/#!2/ace08/1

编辑-做到这一点的不同方法。仅当响应表上的ID按日期/时间顺序排列时,此方法才有效。

SELECT a.id, a.url, MAX(100 * (r2.response_time - r1.response_time) / r2.response_time)
FROM responses r1
INNER JOIN responses r2
ON r1.website_id = r2.website_id
INNER JOIN
(
    SELECT r1.website_id, r1.id, MAX(r2.id) AS prev_id
    FROM responses r1
    INNER JOIN responses r2
    ON r1.website_id = r2.website_id
    AND r1.id > r2.id
    GROUP BY r1.website_id, r1.id
) ordering_query
ON r1.website_id = ordering_query.website_id
AND r1.id = ordering_query.id
AND r2.id = ordering_query.prev_id
INNER JOIN websites a
ON r1.website_id = a.id
GROUP BY a.id, a.url


您可以基于response_time字段而不是id来执行类似的操作,但这将需要response_time来使网站唯一。

编辑

刚刚看到您不只是想要连续的更改,而是想要从最高到最低的响应。假设最低价不一定要紧随最高价:-

SELECT id, url, MAX(100 * (max_response - min_response) / max_response)
FROM
(
    SELECT a.id, a.url, MIN(r1.response_time) AS min_response, MAX(r1.response_time) AS max_response
    FROM responses r1
    INNER JOIN websites a
    ON r1.website_id = a.id
    GROUP BY a.id, a.url
) Sub1


如果您只对较低的响应时间感兴趣,而在较高的响应时间之后:-

SELECT id, url, MAX(100 * (max_response - min_following_response) / max_response)
FROM
(
    SELECT a.id, a.url, MAX(r1.response_time) AS max_response, MIN(r2.response_time) AS min_following_response
    FROM responses r1
    INNER JOIN  responses r2
    ON r1.website_id = r2.website_id 
    AND (r1.created_at < r2.created_at
    OR (r1.created_at = r2.created_at
    AND r1.id < r2.id))
    INNER JOIN websites a
    ON r1.website_id = a.id
    GROUP BY a.id, a.url
) Sub1


(假设响应表上的id字段是唯一的,并且是按顺序创建的)

关于mysql - SQL:找出最大的响应时间变化百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412640/

相关文章:

sql - ORACLE 中的 "less than"用于两个 varchars?

PHP和MYSQL在表中获取随机行

mysql - 改善我的MYSQL查询

mysql - 无法将sequel pro与mysql连接

mysql - 如何使用jsp验证密码并确认注册表单中的密码字段?

php - 从日期表中获取最接近可用时间范围的算法或 SQL

c# - 排行榜,排名查询,如何返回高于/低于用户排名的行

matlab - 将分布拟合到数据 - MATLAB

python - 离散数据的质量

matlab - 如何在Matlab中查看k表示输出?