Mysql - 获取每天每小时的唯一不同 ip 数
Mysql - Get count of unique distinct ip for each hour, each day
要问的更新问题:
我有一个带有日期和 ip 的 table。我想提取每小时新的非重复不同 ip 的计数,而不是不同 ip 聚合小时的计数
我的table长得像
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)
我期望的结果是:
日期小时计数
2018 年 7 月 1 日 08:00:00 1
2018 年 7 月 2 日 09:00:00 0
2018 年 7 月 2 日 10:00:00 1
2018 年 7 月 3 日 11:00:00 0
2018 年 7 月 3 日 12:00:00 0
2018 年 7 月 3 日 13:00:00 1
Vlam 的查询适用于每天的唯一计数,
SELECT aa.date, count(distinct aa.ip) as count_ips FROM table1 aa
哪里不存在
(select bb.ip 来自 table1 bb 其中 bb.ip = aa.ip 和 bb.date < aa.date)
分组依据 aa.date 排序依据 aa.date asc;
现在我想细分到每个小时。有什么建议吗?
提前致谢!
我已经修改了原来的 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;
要问的更新问题: 我有一个带有日期和 ip 的 table。我想提取每小时新的非重复不同 ip 的计数,而不是不同 ip 聚合小时的计数
我的table长得像
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)
我期望的结果是: 日期小时计数 2018 年 7 月 1 日 08:00:00 1 2018 年 7 月 2 日 09:00:00 0 2018 年 7 月 2 日 10:00:00 1 2018 年 7 月 3 日 11:00:00 0 2018 年 7 月 3 日 12:00:00 0 2018 年 7 月 3 日 13:00:00 1
Vlam 的查询适用于每天的唯一计数, SELECT aa.date, count(distinct aa.ip) as count_ips FROM table1 aa 哪里不存在 (select bb.ip 来自 table1 bb 其中 bb.ip = aa.ip 和 bb.date < aa.date) 分组依据 aa.date 排序依据 aa.date asc;
现在我想细分到每个小时。有什么建议吗?
提前致谢!
我已经修改了原来的 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;