要问的最新问题:我有一张有日期和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)
我期望的结果是:日期小时计数2018年1月7日08:00:00 12018年2月7日09:00:002018年2月7日10:00:00 12018年3月7日11:00:002018年3月7日12:00:002018年3月7日13:00:00 1
Vlam的查询适用于每天的唯一计数,从表1 aa中选择aa.date,count(不同的aa.ip(作为count_ips其中不存在(从表1 bb中选择bb.ip,其中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;