2017-01-19 79 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個不同的東西? –