简体   繁体   English

计算每天两个日期之间的时间

[英]Calculating time between two dates per day

I'm working on a project where I search for a wifi signal (from cellphones etc). 我正在研究一个项目(从手机等设备中)搜索wifi信号的项目。 It detects every mac address that is around in the wifi sensors radius. 它检测在wifi传感器半径内的每个mac地址。 This data is then sent from a server to a database, which uses a reporting tool to show statistics. 然后,这些数据从服务器发送到数据库,该数据库使用报告工具显示统计信息。 This can be used in stores to study customer behavior. 可以在商店中使用它来研究客户行为。

This is what the data looks like: 数据如下所示:

在此处输入图片说明

The thing is, I want to know the time between entries, The problem is if a person stays for 10 minutes in the store it wil display alot of addresses, what I want to calculate is the difference between the visits. 问题是,我想知道两次输入之间的时间,问题是,如果一个人在商店中停留10分钟,它将显示很多地址,我想计算的是两次访问之间的差额。 So what I need to do is count the time between the current day and the next day that they came. 因此,我需要做的是计算当天到第二天之间的时间。

I would then like to display this in a table like the one below. 然后,我想在下面的表格中显示此内容。

current time       |  next time they came  |  Time between visist
-------------------------------------------------------------------
*current time*     |  *other day*          |  *Time between visits*

I have no idea how I should do this, abstract thinking is always welcome 我不知道该怎么做,总是欢迎抽象思维

Ps If there is any missing info, please comment. 附言:如果缺少任何信息,请发表评论。 I'm new to the forums. 我是新来的论坛。

First of all you have to translate that time field into its readable date part 首先,您必须将time字段转换为其可读的日期部分

select date(from_unixtime(`time`)) from yourTable;

This value can be the joining criteria of a self join 此值可以是自我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`)

This would get you, for each address and each visit time, the earliest visit time on a different day. 对于每个地址和每个访问时间,这将使您在不同的日期获得最早的访问时间。

From there you could return the time difference 从那里您可以返回时差

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

The coalesce is to avoid having a null in the timediff function, which would happen for each address's last visit. coalesce是为了避免在timediff函数中为null ,这将在每个地址的最后一次访问中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM