MySQL获得最后N行并找到百分比

时间:2018-02-12 11:15:12

标签: mysql sql database

我有一个MySQL表,其中有数千行和char值列:

+----+----------+----------+---------+---------+---------+---------+---------+---------+
| id | date     | time     | Server1 | Server2 | Server3 | Server4 | Server5 | Server6 |
+----+----------+----------+---------+---------+---------+---------+---------+---------+
| 52 | 12/02/18 | 14:00:00 |       1 |       1 |       0 |       1 |       1 |       1 |
| 51 | 12/02/18 | 13:55:00 |       1 |       1 |       1 |       1 |       0 |       1 |
| 50 | 12/02/18 | 13:52:00 |       0 |       1 |       1 |       1 |       1 |       1 |
| 49 | 12/02/18 | 13:50:00 |       1 |       1 |       1 |       1 |       0 |       1 |
| 48 | 12/02/18 | 13:45:00 |       1 |       1 |       1 |       1 |       1 |       0 |
| 47 | 12/02/18 | 13:40:00 |       1 |       0 |       1 |       0 |       1 |       1 |
| 46 | 12/02/18 | 13:35:00 |       1 |       1 |       1 |       1 |       0 |       1 |
| 45 | 12/02/18 | 13:30:00 |       1 |       1 |       0 |       1 |       1 |       1 |
| 44 | 12/02/18 | 13:27:50 |       0 |       1 |       1 |       0 |       1 |       1 |
| 43 | 12/02/18 | 13:27:14 |       0 |       1 |       1 |       1 |       1 |       1 |
+----+----------+----------+---------+---------+---------+---------+---------+---------+

我做了很多研究,但无法在网上找到任何内容,我需要打印出最后5行并获得每个服务器列上1和0的百分比,如下所示或任何将向他们展示的方式。

+------+---------+---------+---------+---------+---------+---------+
|      | Server1 | Server2 | Server3 | Server4 | Server5 | Server6 |
+------+---------+---------+---------+---------+---------+---------+
|    0 |       10|       2 |       7 |   8.443 |  11.155 |  99.921 |
|    1 |  11.411 |  12.443 |       1 |       1 |       1 |       1 |
+------+---------+---------+---------+---------+---------+---------+

谢谢..

1 个答案:

答案 0 :(得分:2)

不是最具视觉启发性的查询,但我们可以只使用两个查询的并集,分别查找0和1的统计信息。我下面的子查询使用t别名来使用LIMIT来识别表中的最后5行(或者前5行,按日期和时间按顺序递减)。

SELECT 0 AS stat,
    100.0* SUM(CASE WHEN Server1 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server1,
    100.0* SUM(CASE WHEN Server2 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server2,
    100.0* SUM(CASE WHEN Server3 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server3,
    100.0* SUM(CASE WHEN Server4 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server4,
    100.0* SUM(CASE WHEN Server5 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server5,
    100.0* SUM(CASE WHEN Server6 = 0 THEN 1 ELSE 0 END) / COUNT(*) AS Server6
FROM
(
    SELECT Server1, Server2, Server3, Server4, Server5, Server6
    FROM yourTable
    ORDER BY date DESC, time DESC
    LIMIT 5
) t
UNION ALL
SELECT 1,
    100.0* SUM(Server1) / COUNT(*),
    100.0* SUM(Server2) / COUNT(*),
    100.0* SUM(Server3) / COUNT(*),
    100.0* SUM(Server4) / COUNT(*),
    100.0* SUM(Server5) / COUNT(*),
    100.0* SUM(Server6) / COUNT(*)
FROM
(
    SELECT Server1, Server2, Server3, Server4, Server5, Server6
    FROM yourTable
    ORDER BY date DESC, time DESC
    LIMIT 5
) t;

注意:我认为您的date列实际上是日期,而不仅仅是文字。如果是文字,那么您必须先使用STR_TO_DATE将其转换为日期,例如

STR_TO_DATE(date, '%d/%m/%y')