gpt4 book ai didi

mysql order by sum(column) 由具有自连接的另一列分组

转载 作者:行者123 更新时间:2023-11-30 21:39:45 24 4
gpt4 key购买 nike

我有表事件:

create table events (
time TIMESTAMP NOT NULL,
passes INT UNSIGNED NOT NULL,
fails INT UNSIGNED NOT NULL,
device_channel VARCHAR(5) NOT NULL,
device_name VARCHAR(5) NOT NULL,
events_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);

包含以下数据:

INSERT INTO events VALUES 
('2018-08-08 10:30:00',3000,15,'ch1','dev1',NULL),
('2018-08-08 10:30:00',3000,12,'ch1','dev2',NULL),
('2018-08-08 10:30:00',3000,9,'ch1','dev3',NULL),
('2018-08-08 10:30:00',3000,11,'ch2','dev4',NULL),
('2018-08-08 10:30:00',3000,10,'ch2','dev5',NULL),
('2018-08-08 10:30:00',3000,8,'ch2','dev6',NULL),

('2018-08-08 10:45:00',4000,18,'ch1','dev1',NULL),
('2018-08-08 10:45:00',4000,16,'ch1','dev2',NULL),
('2018-08-08 10:45:00',4000,10,'ch1','dev3',NULL),
('2018-08-08 10:45:00',4000,11,'ch2','dev4',NULL),
('2018-08-08 10:45:00',4000,20,'ch2','dev5',NULL),
('2018-08-08 10:45:00',4000,10,'ch2','dev6',NULL);

我需要显示时间间隔内通过和失败列的差异,并创建了以下查询:

SELECT a.time AS Start_time, b.time AS End_time ,a.device_channel AS Channel,a.device_name AS Device,b.passes-a.passes AS Passes, b.fails-a.fails AS Fails
FROM events a
JOIN events b
ON b.time > a.time AND b.device_name = a.device_name;

此查询生成以下结果:

+---------------------+---------------------+---------+--------+--------+-------+
| Start_time | End_time | Channel | Device | Passes | Fails |
+---------------------+---------------------+---------+--------+--------+-------+
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev1 | 1000 | 3 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev2 | 1000 | 4 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev3 | 1000 | 1 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev4 | 1000 | 0 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev5 | 1000 | 10 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev6 | 1000 | 2 |
+---------------------+---------------------+---------+--------+--------+-------+
6 rows in set (0.00 sec)

但是,我需要对在 device_channel 上分组的 SUM(Fails) 上的表进行排序,以便在查询后该表最终如下所示:

+---------------------+---------------------+---------+--------+--------+-------+
| Start_time | End_time | Channel | Device | Passes | Fails |
+---------------------+---------------------+---------+--------+--------+-------+
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev4 | 1000 | 0 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev5 | 1000 | 10 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev6 | 1000 | 2 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev1 | 1000 | 3 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev2 | 1000 | 4 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev3 | 1000 | 1 |
+---------------------+---------------------+---------+--------+--------+-------+
6 rows in set (0.00 sec)

我尝试将以下 ORDER BY 子句附加到查询中,但它似乎不起作用:

  ORDER BY SUM(b.fails-a.fails) OVER (PARTITION BY channel.channel_num) DESC;

非常感谢任何帮助。

根据@vinay Chhabra 的输入,我创建了一个提供所需结果的查询:

SELECT t1.*
FROM
(
SELECT a.time AS Start_time, b.time AS End_time ,a.device_channel AS Channel,a.device_name AS Device, b.fails-a.fails AS Fails
FROM events a
JOIN events b
ON b.time > a.time AND b.device_name = a.device_name
) AS t1

JOIN
(
SELECT a.device_channel AS Channel,a.device_name AS Device, b.fails-a.fails AS Fails, SUM(b.fails-a.fails) AS Sumfails
FROM events a
JOIN events b
ON b.time > a.time AND b.device_name = a.device_name
Group by Channel
) t2
ON t1.Channel = t2.Channel
ORDER by Sumfails DESC, Fails DESC;

+---------------------+---------------------+---------+--------+-------+
| Start_time | End_time | Channel | Device | Fails |
+---------------------+---------------------+---------+--------+-------+
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev5 | 10 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev6 | 2 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch2 | dev4 | 0 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev2 | 4 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev1 | 3 |
| 2018-08-08 10:30:00 | 2018-08-08 10:45:00 | ch1 | dev3 | 1 |
+---------------------+---------------------+---------+--------+-------+
6 rows in set (0.00 sec)

不过我怀疑有更优雅的解决方案。

最佳答案

这是您要的吗?试试看:-

SELECT a.time AS Start_time, b.time AS End_time ,a.device_channel AS Channel,a.device_name AS Device,b.passes-a.passes AS Passes, b.fails-a.fails AS Fails
FROM events a
JOIN events b
ON b.time > a.time AND b.device_name = a.device_name
ORDER BY fails desc;

对于 channel 而言:-

SELECT a.time AS Start_time, b.time AS End_time ,a.device_channel AS Channel,a.device_name AS Device,b.passes-a.passes AS Passes, b.fails-a.fails AS Fails
FROM events a
JOIN events b
ON b.time > a.time AND b.device_name = a.device_name
ORDER BY channel ,fails desc;

关于mysql order by sum(column) 由具有自连接的另一列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52220679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com