2009-08-14 75 views
0

2表與以下模型:SQL時間差

Id: long unique; timestamp:long; price:double 

兩個表中的數據是除時間戳相同。 時間戳記爲unix_timems

問題:有多少對的時間差大於1000ms?

+0

一般而言,您應該提及您在問題中使用的SQL方言。 – Thorarin 2009-08-14 05:16:37

+0

@Thorain,同意。 – Siv 2009-08-14 05:20:26

回答

1

試試這個:

SELECT COUNT(*) 
FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.id 
WHERE 
    (t1.timestamp - t2.timestamp > 1000) or 
    (t2.timestamp - t1.timestamp > 1000) 
+0

@Rax,嘗試了您的查詢。它顯示所需的結果。萬分感謝 ! – Siv 2009-08-14 05:26:27

0

假設ID是相同的兩個表和時間戳是唯一的不同領域

假設這些都是你的表

ID | timestamp | price 

我們會打電話給他們表1和表2

select count(*) from table1 t1 
inner join table2 t2 on t1.ID=t2.ID 
where (t1.timestamp-t2.timestamp) > 1000 
or (t2.timestamp-t1.timestamp)>1000 
+0

@Saeros,我試過了你的查詢,但我不需要羣組。我只需要計數,並且Id列也有唯一的權利。謝謝你的回答! – Siv 2009-08-14 05:27:33