2017-01-19 81 views
0

数据我有这个表:如何收集对基于时间戳

enter image description here

我应该为了得到在长达1分钟的时间内所发生的输入所有对书写的内容查询。

,我需要从上表得到的最终结果应该是:

enter image description here

+0

你已经用mysql和BigQuery标记了这个问题 - 这是什么? –

+1

提供DDL来创建表并提供插入语句来填充表中的数据总是很好。 – TechEnthusiast

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 –

回答

0

我这样做是在T-SQL,但我认为它不是很难在MySQL翻译。 假设timestamp列是时间类型那么这个查询做什么你所要求的(我在T-SQL测试,所以我创建你必须使用你的表的表变量):

declare @table table (userid int, input varchar(50),timestmp datetime) 
insert into @table values(1,'hy','1/19/2017 12:00') 
insert into @table values(2,'by','1/19/2017 12:00') 
insert into @table values(1,'hy2','1/19/2017 12:01') 
insert into @table values(2,'by2','1/19/2017 12:01') 
insert into @table values(3,'why','1/19/2017 12:01') 
select t1.userid,t1.input,t2.input from @table t1 inner join @table t2 on t1.userid=t2.userid and t1.timestmp=DATEADD(mi,-1,t2.timestmp) 
0

尝试此查询,它会给出预期的产出。

SELECT t1.userid,t1.input,t2.input 
     FROM test_table t1 
     INNER JOIN test_table t2 
     ON TIMESTAMPDIFF(MINUTE,t1.timestmp,t2.timestmp) <= 1 
      AND t1.userid = t2.userid 

注:与test_table

更换你table_name的任何疑问的情况下提出。

+0

与上面的回答相同 –

+0

是不是t-sql查询和mysql查询2个不同的东西? –