我在运行 MySQL 的表中有一些 SQL 数据,下面是我使用 MySQL WorkBench 导出时的样子。
我看到了 Count number of rows that are not within 10 seconds of each other发布并尝试在此处相应地应用它,但我无法轻易弄清楚,所以我作为问题发布以寻求帮助。
这是我在表格中开始的数据
Column Headers
TimeInt, TimeStr, IsInItValue, Value, IQuality
数据
'1495542477', '2017-05-23 12:27:57', '0', '0', '3'
'1495542475', '2017-05-23 12:27:55', '0', '1', '3'
'1495542474', '2017-05-23 12:27:54', '0', '3', '3'
'1495542473', '2017-05-23 12:27:53', '0', '4', '3'
'1495542472', '2017-05-23 12:27:52', '0', '5', '3'
'1495542471', '2017-05-23 12:27:51', '0', '4', '3'
'1495542470', '2017-05-23 12:27:50', '0', '3', '3'
'1495447612', '2017-05-22 10:06:52', '0', '1', '3'
'1495447611', '2017-05-22 10:06:51', '0', '2', '3'
'1494851001', '2017-05-15 12:23:21', '0', '2', '3'
'1493819613', '2017-05-03 13:53:33', '0', '0', '3'
'1493819612', '2017-05-03 13:53:32', '0', '1', '3'
'1493819611', '2017-05-03 13:53:31', '0', '2', '3'
'1493819609', '2017-05-03 13:53:29', '0', '4', '3'
'1493819608', '2017-05-03 13:53:28', '0', '6', '3'
'1493819607', '2017-05-03 13:53:27', '0', '5', '3'
'1493819606', '2017-05-03 13:53:26', '0', '4', '3'
'1493819605', '2017-05-03 13:53:25', '0', '2', '3'
'1493819603', '2017-05-03 13:53:23', '0', '1', '3'
我想看看是否有办法将这些数据连接到自身或通过子查询应用一些逻辑等,这样我就可以取回值或轻松区分下一行的时间戳所在的数据在此表中大于 10 秒。
我在上面链接的帖子中的一个答案的方法使用了一种方法,其中具有 NULL 值的记录会有所帮助,但我无法让它与 MySQL 一起工作。
我尝试了 Count number of rows that are not within 10 seconds of each other 上答案的变体发布但无法弄清楚我哪里出错了但低于我尝试过的少数几件事之一。我不确定 interval 是否仍然有效,因为那是从 2011 年开始的,或者我做错了其他事情导致语法错误,但这是我尝试过的事情之一。
SELECT t2.TimeStr, Value,
(SELECT MAX(t.TimeStr)
FROM canouncebit t
WHERE t.TimeStr > t2.TimeStr
AND t.TimeStr - t2.TimeStr <= interval '10' second) NextRecord
FROM canouncebit t2
ORDER BY TimeStr
我想以这样的方式结束,甚至有点接近我可以使用
数据
'1495542477', '2017-05-23 12:27:57', '0', '0', '3'
'1495447612', '2017-05-22 10:06:52', '0', '1', '3'
'1494851001', '2017-05-15 12:23:21', '0', '2', '3'
'1493819613', '2017-05-03 13:53:33', '0', '0', '3'
所以基本上我只需要取回数据,特别是只取回该值已存在超过 X 秒(在本例中为 10 秒)的记录的值。
CREATE TABLE `ContainerOzBit` (
`TimeInt` varchar(10) NOT NULL,
`TimeStr` datetime NOT NULL,
`IsInitValue` int(11) NOT NULL,
`Value` float NOT NULL,
`IQuality` int(11) NOT NULL,
UNIQUE KEY `TimeInt` (`TimeInt`,`TimeStr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542477','2017-05-23 12:27:57',0,0,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542475','2017-05-23 12:27:55',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542474','2017-05-23 12:27:54',0,3,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542473','2017-05-23 12:27:53',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542472','2017-05-23 12:27:52',0,5,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542471','2017-05-23 12:27:51',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495542470','2017-05-23 12:27:50',0,3,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495447612','2017-05-22 10:06:52',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1495447611','2017-05-22 10:06:51',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1494851001','2017-05-15 12:23:21',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819613','2017-05-03 13:53:33',0,0,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819612','2017-05-03 13:53:32',0,1,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819611','2017-05-03 13:53:31',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819609','2017-05-03 13:53:29',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819608','2017-05-03 13:53:28',0,6,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819607','2017-05-03 13:53:27',0,5,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819606','2017-05-03 13:53:26',0,4,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819605','2017-05-03 13:53:25',0,2,3);
INSERT INTO `ContainerOzBit` (`TimeInt`,`TimeStr`,`IsInitValue`,`Value`,`IQuality`) VALUES ('1493819603','2017-05-03 13:53:23',0,1,3);
最佳答案
一种方法是利用 MySQL 的 session 变量:
-- omit the duplicated timeint that we used in the derived table
SELECT c.* FROM (
-- grab the maximum timeint value in the group
SELECT MAX(timeint) timeint FROM (
SELECT timeint, -- current row's timeint value
-- if diff betwen current and prev values more than 10 sec
-- increment the group number, otherwise keep it the same
@g := IF(timeint - @p > 10, @g + 1, @g) g,
-- preserve the the value so it's available on the next iteration
@p := timeint
FROM ContainerOzBit CROSS JOIN (
SELECT @p := NULL, @g := 1 -- initialize sesion variables
) i
-- deterministic order is crucial for this approach
-- since we're iteration row by row
ORDER BY timeint
) q
-- group by the group number
GROUP BY g
-- since timeint values are unique
-- join back and retrieve all the columns
) r JOIN ContainerOzBit c
ON r.timeint = c.timeint
-- set the reverse order for the result set
ORDER BY timeint DESC;
阅读来自最内在选择的评论
结果:
+------------+---------------------+-------------+-------+----------+ | TimeInt | TimeStr | IsInitValue | Value | IQuality | +------------+---------------------+-------------+-------+----------+ | 1495542477 | 2017-05-23 12:27:57 | 0 | 0 | 3 | | 1495447612 | 2017-05-22 10:06:52 | 0 | 1 | 3 | | 1494851001 | 2017-05-15 12:23:21 | 0 | 2 | 3 | | 1493819613 | 2017-05-03 13:53:33 | 0 | 0 | 3 | +------------+---------------------+-------------+-------+----------+
这是一个dbfiddle演示
关于mysql - 带有时间戳的记录,每部分 10 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272815/
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame