Mysql-每天每小时获取唯一唯一IP的计数

时间:2018-08-01 01:10:39

标签: mysql distinct

要更新的问题: 我有一张带日期和IP的表格。我想拉出每小时新的非重复的不同IP的计数,而不是每小时不同IP的合计计数

我的桌子看起来像

Date          Time              IP
07-01-2018  08:00:00           1.1.1
07-02-2018  09:00:00           1.1.1
07-02-2018  10:00:00           2.2.2
07-03-2018  11:00:00           1.1.1
07-03-2018  12:00:00           2.2.2
07-03-2018  13:00:00           3.3.3

然后打开

07-01-2018 08:00:00, distinct count of ip should be 1 (1.1.1)
07-02-2018 09:00:00, new distinct count of ip should be 0
07-02-2018 10:00:00, new distinct count of ip should be 1 (2.2.2) 
07-03-2018 11:00:00, new distinct count of ip should be 0
07-03-2018 12:00:00, new distinct count of ip should be 0
07-03-2018 13:00:00, new distinct count of ip should be 1 (3.3.3)

我期望的结果是: 日期小时计数 07-01-2018 08:00:00 1 07-02-2018 09:00:00 0 07-02-2018 10:00:00 1 07-03-2018 11:00:00 0 07-03-2018 12:00:00 0 07-03-2018 13:00:00 1

Vlam的查询每天都可用于唯一计数,     SELECT aa.date,count(distinct aa.ip)as count_ips FROM table1 aa 不存在的地方 (从表1 bb中选择bb.ip,其中bb.ip = aa.ip且bb.date

现在我想细分到每个小时。有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我已经修改了原始SQL,只是添加了一个过滤条件以删除该行中该日期之前出现的所有IP地址,因为仅应计算新IP地址。

SELECT aa.date, count(distinct aa.ip) as count_ips FROM table1 aa
where not exists
(select bb.ip from table1 bb where bb.ip = aa.ip and bb.date < aa.date)
group by aa.date order by aa.date asc;