[英]Calculating time between two dates per day
我正在研究一個項目(從手機等設備中)搜索wifi信號的項目。 它檢測在wifi傳感器半徑內的每個mac地址。 然后,這些數據從服務器發送到數據庫,該數據庫使用報告工具顯示統計信息。 可以在商店中使用它來研究客戶行為。
數據如下所示:
問題是,我想知道兩次輸入之間的時間,問題是,如果一個人在商店中停留10分鍾,它將顯示很多地址,我想計算的是兩次訪問之間的差額。 因此,我需要做的是計算當天到第二天之間的時間。
然后,我想在下面的表格中顯示此內容。
current time | next time they came | Time between visist
-------------------------------------------------------------------
*current time* | *other day* | *Time between visits*
我不知道該怎么做,總是歡迎抽象思維
附言:如果缺少任何信息,請發表評論。 我是新來的論壇。
首先,您必須將time
字段轉換為其可讀的日期部分
select date(from_unixtime(`time`)) from yourTable;
此值可以是自我join
的聯接條件
select t1.address,
from_unixtime(t1.`time`),
min(from_unixtime(t2.`time`))
from yourTable t1
left join
yourTable t2
on t1.address = t2.address and
date(from_unixtime(t1.`time`)) < date(from_unixtime(t2.`time`))
group by t1.address, from_unixtime(t1.`time`)
對於每個地址和每個訪問時間,這將使您在不同的日期獲得最早的訪問時間。
從那里您可以返回時差
select tt.address,
tt.visit,
tt.next_visit,
timediff(coalesce(tt.next_visit, tt.visit), tt.visit) as `interval`
from (
select t1.address,
from_unixtime(t1.`time`) as visit,
min(from_unixtime(t2.`time`)) as next_visit
from yourTable t1
left join
yourTable t2
on t1.address = t2.address and
date(from_unixtime(t1.`time`)) < date(from_unixtime(t2.`time`))
group by t1.address, from_unixtime(t1.`time`)
) tt
coalesce
是為了避免在timediff
函數中為null
,這將在每個地址的最后一次訪問中發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.