查找每种产品的总和和唯一命中

时间:2011-06-24 04:09:22

标签: mysql sql

我的表结构如下

    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

的标准

Total Hits =属于特定产品的所有记录

独特命中= 2次命中被识别为唯一命中如果(1)IP不同或(2)对于相同的ip,命中时间差异为5分钟

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分: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的想法所做的改变:

  1. 将ceil()更改为round()因为 ceil / floor将导致边缘情况发生 处理不当
  2. 乘以该轮的结果() 我认为rMX意味着这样做 只是忘了输入它。
  3. 编辑:实际上没有必要乘以5。我的大脑只是混乱。将ceil()更改为round()仍然很重要。

答案 1 :(得分:1)

<强> UPD&GT;

select productid, count(*) totalhits, 
    count(distinct 
        concat(ip,
            date_format(hittime, '%Y%m%d%H'),
            ceil(date_format(hittime, '%i') / 5))
        ) uniquehits
from table
group by productid

我认为,这应该有效。对不起,没时间测试了。