我的表结构如下
id productid ip hittime
---------------------------------------------------------------------------
1 5 1.1.1.1 2011-05-03 06:55:11
2 5 1.1.1.1 2011-05-03 06:57:11
3 6 2.2.2.2 2011-05-03 07:30:00
4 4 1.1.1.1 2011-05-03 07:32:54
5 5 2.2.2.2 2011-05-03 07:55:00
现在,我需要查询,以便为我输出每种产品的总点击数和唯一点击数
productid totalhits uniquehits
------------------------------------------------------------------
4 1 1
5 3 2
6 1 1
准则
总点击数=属于特定产品的所有记录
唯一匹配数=如果(1)IP不同或(2)对于相同的ip,匹配时间相差5分钟,则将2个匹配点识别为唯一匹配数
我该如何实现?
解决方法:
rMX与他的解决方案非常接近,非常聪明.他应该得到信誉,我只是对其进行了一些微调,以添加一些缺失的部分:
select productid, count(*) totalhits,
count(distinct
concat(ip,
date_format(hittime, '%Y%m%d%H'),
round(date_format(hittime, '%i') / 5) * 5)
) uniquehits
from table
group by productid
我对rMX的想法进行了更改:
>将ceil()更改为round(),因为
天花板/地板会导致边缘情况
不当对待
>将round()的结果相乘
5.我认为rMX就是要这样做
只是忘了输入.
编辑:乘以5确实是没有必要的.我的大脑只是糊涂了.不过,将ceil()更改为round()仍然很重要.
标签:sql,mysql
来源: https://codeday.me/bug/20191208/2090026.html