我正在创建一个“每小时一小时”的报告,该报告显示每小时登录系统的用户数量,我希望这是一个独特的计数。
然而,当我运行下面的SQL时,它会相互添加1个,例如12am = 5时真实数字是4,1am = 10时真实数字是9。
我无法弄清楚为什么会这样。下面有一个链接显示我的结果看起来如何,但它显示的信息不正确。
以下是我的查询。
SELECT
count(distinct IF(loginDate BETWEEN '2017-02-08 00:00:00' AND '2017-02-08 00:59:59',
(Userid),
0)) AS '12am',
count(distinct IF(loginDate BETWEEN '2017-02-08 01:00:00' AND '2017-02-08 01:59:59',
(Userid),
0)) AS '1am',
count(distinct IF(loginDate BETWEEN '2017-02-08 02:00:00' AND '2017-02-08 02:59:59',
(Userid),
0)) AS '2am',
count(distinct IF(loginDate BETWEEN '2017-02-08 03:00:00' AND '2017-02-08 03:59:59',
(Userid),
0)) AS '3am',
count(distinct IF(loginDate BETWEEN '2017-02-08 04:00:00' AND '2017-02-08 04:59:59',
(Userid),
0)) AS '4am',
count(distinct IF(loginDate BETWEEN '2017-02-08 05:00:00' AND '2017-02-08 05:59:59',
(Userid),
0)) AS '5am',
count(distinct IF(loginDate BETWEEN '2017-02-08 06:00:00' AND '2017-02-08 06:59:59',
(Userid),
0)) AS '6am',
count(distinct IF(loginDate BETWEEN '2017-02-08 07:00:00' AND '2017-02-08 07:59:59',
(Userid),
0)) AS '7am',
count(distinct IF(loginDate BETWEEN '2017-02-08 08:00:00' AND '2017-02-08 08:59:59',
(Userid),
0)) AS '8am',
count(distinct IF(loginDate BETWEEN '2017-02-08 09:00:00' AND '2017-02-08 09:59:59',
(Userid),
0)) AS '9am',
count(distinct IF(loginDate BETWEEN '2017-02-08 10:00:00' AND '2017-02-08 10:59:59',
(Userid),
0)) AS '10am',
count(distinct IF(loginDate BETWEEN '2017-02-08 11:00:00' AND '2017-02-08 11:59:59',
(Userid),
0)) AS '11am',
count(distinct IF(loginDate BETWEEN '2017-02-08 12:00:00' AND '2017-02-08 12:59:59',
(Userid),
0)) AS '12pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 13:00:00' AND '2017-02-08 13:59:59',
(Userid),
0)) AS '1pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 14:00:00' AND '2017-02-08 14:59:59',
(Userid),
0)) AS '2pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 15:00:00' AND '2017-02-08 15:59:59',
(Userid),
0)) AS '3pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 16:00:00' AND '2017-02-08 16:59:59',
(Userid),
0)) AS '4pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 17:00:00' AND '2017-02-08 17:59:59',
(Userid),
0)) AS '5pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 18:00:00' AND '2017-02-08 18:59:59',
(Userid),
0)) AS '6pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 19:00:00' AND '2017-02-08 19:59:59',
(Userid),
0)) AS '7pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 20:00:00' AND '2017-02-08 20:59:59',
(Userid),
0)) AS '8pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 21:00:00' AND '2017-02-08 21:59:59',
(Userid),
0)) AS '9pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 22:00:00' AND '2017-02-08 22:59:59',
(Userid),
0)) AS '10pm',
count(distinct IF(loginDate BETWEEN '2017-02-08 23:00:00' AND '2017-02-08 23:59:59',
(Userid),
0)) AS '11pm'
FROM
mb_useraccessheader
答案 0 :(得分:1)
因为你的If
条件。你在说distinct (If(true,userid,0))
。由于0
是一个值,因此它被视为distinct
值。
解决方案:将其替换为null