我正在尝试生成具有稳定时间线的动态图表,因此我需要在x轴上添加额外的时间(分钟)。 我需要2列。第1列(时间)显示一天中的每一分钟,该表只有一个时间列,从00:00到23:59列出(示例表只有20分钟)。我正在尝试使用'log'表中的结果加入此项。该查询将过滤我正在查看的机器名称,例如Machine'name''1'。然后列出此机器在“TimeMins”表中的1440分钟后改变状态。我希望有帮助吗? 最后,我有一个动态图表,显示整个机器一整天的状态变化。 'time'列将为图表设置x轴,给出稳定的时间线
不幸的是我无法正常加入。也许有另一种方式或者有人可以解决这个问题吗?
结果应类似于......
time state
00:00 null (or zero)
00:01 null
00:02 1
00:03 null
00:04 null
00:05 0
00:06 null
00:07 1
等。一天中每分钟的值,是否在该日期的“日志”中产生结果。
非常感谢。已经有2天以上了。
这是SQLfiddle:
http://sqlfiddle.com/#!9/5f91a0/2
感谢。 杰米。
答案 0 :(得分:0)
您可以使用内部选择。
在下面的示例中,我使用的是3.
select name, beginning, ending - beginning as seconds from (
select a.name, a.ts as beginning, (
select min(b.ts)
from log b
where b.ts > a.ts and a.name = b.name and b.state = 0
) as ending
from log a
where a.state = 1
) c
order by beginning;
第二个是为每台机器提供开始和结束
第一个包装第二个,它只计算每次运行的秒数,并按开始
对结果进行排序当机器在a.ts开始后停止并且状态0具有相同的名称(以匹配同一台机器)时,第三个试图找到下一行
select min(b.ts)
from log b
where b.ts > a.ts and a.name = b.name and b.state = 0
答案 1 :(得分:0)
我并不是在提倡这个解决方案(即使使用这种方法,肯定有更优化的方式来编写这个查询),但仅仅是为了说明...
DROP TABLE IF EXISTS `log`;
CREATE TABLE `log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(16) DEFAULT NULL,
`ts` datetime NOT NULL,
`state` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `log` VALUES
(20,1,'2016-05-16 00:03:02',1),
(21,1,'2016-05-16 00:04:03',0),
(22,2,'2016-05-16 00:04:28',1),
(23,2,'2016-05-16 00:06:45',0),
(25,1,'2016-05-16 00:14:50',1),
(26,2,'2016-05-16 00:15:35',1);
DROP TABLE IF EXISTS `TimeMins`;
CREATE TABLE `TimeMins` (
t time PRIMARY KEY
);
INSERT INTO `TimeMins` VALUES
('00:00:00'),
('00:01:00'),
('00:02:00'),
('00:03:00'),
('00:04:00'),
('00:05:00'),
('00:06:00'),
('00:07:00'),
('00:08:00'),
('00:09:00'),
('00:10:00'),
('00:11:00'),
('00:12:00'),
('00:13:00'),
('00:14:00'),
('00:15:00'),
('00:16:00'),
('00:17:00'),
('00:18:00'),
('00:19:00'),
('00:20:00');
SELECT a.*
, b.id
, b.name
, b.state
FROM
( SELECT DISTINCT CONCAT(DATE(l.ts),' ',t.t) i
FROM log l
, timemins t
) a
LEFT
JOIN log b
ON DATE_FORMAT(b.ts,'%Y%m%d%h%i') = DATE_FORMAT(a.i,'%Y%m%d%h%i');
+---------------------+------+------+-------+
| i | id | name | state |
+---------------------+------+------+-------+
| 2016-05-16 00:00:00 | NULL | NULL | NULL |
| 2016-05-16 00:01:00 | NULL | NULL | NULL |
| 2016-05-16 00:02:00 | NULL | NULL | NULL |
| 2016-05-16 00:03:00 | 20 | 1 | 1 |
| 2016-05-16 00:04:00 | 21 | 1 | 0 |
| 2016-05-16 00:04:00 | 22 | 2 | 1 |
| 2016-05-16 00:05:00 | NULL | NULL | NULL |
| 2016-05-16 00:06:00 | 23 | 2 | 0 |
| 2016-05-16 00:07:00 | NULL | NULL | NULL |
| 2016-05-16 00:08:00 | NULL | NULL | NULL |
| 2016-05-16 00:09:00 | NULL | NULL | NULL |
| 2016-05-16 00:10:00 | NULL | NULL | NULL |
| 2016-05-16 00:11:00 | NULL | NULL | NULL |
| 2016-05-16 00:12:00 | NULL | NULL | NULL |
| 2016-05-16 00:13:00 | NULL | NULL | NULL |
| 2016-05-16 00:14:00 | 25 | 1 | 1 |
| 2016-05-16 00:15:00 | 26 | 2 | 1 |
| 2016-05-16 00:16:00 | NULL | NULL | NULL |
| 2016-05-16 00:17:00 | NULL | NULL | NULL |
| 2016-05-16 00:18:00 | NULL | NULL | NULL |
| 2016-05-16 00:19:00 | NULL | NULL | NULL |
| 2016-05-16 00:20:00 | NULL | NULL | NULL |
+---------------------+------+------+-------+