Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果该值以前未出现,则为MySQL计数_Mysql_Sql_Count_Distinct Values - Fatal编程技术网

如果该值以前未出现,则为MySQL计数

如果该值以前未出现,则为MySQL计数,mysql,sql,count,distinct-values,Mysql,Sql,Count,Distinct Values,以下是用户、购物车、和时间的列表。 一个用户可以有多个购物车。所有匿名用户都具有用户ID=1000;任何识别的用户的ID都不同于1000 所有cartIds都是唯一的 +------------+-------------+----------------------+ | userId | cartId | time | +------------+-------------+----------------------+ | 7650

以下是
用户、购物车、
时间的列表。
一个
用户
可以有多个
购物车
。所有匿名
用户
都具有
用户ID=1000
;任何识别的用户的ID都不同于
1000

所有
cartId
s都是唯一的

+------------+-------------+----------------------+
| userId     | cartId      | time                 | 
+------------+-------------+----------------------+
| 7650       | 231         | 2014-08-27 13:41:02  |
+------------+-------------+----------------------+
| 7632       | 221         | 2014-08-27 13:42:02  |
+------------+-------------+----------------------+
| 7650       | 289         | 2014-08-27 14:13:02  |
+------------+-------------+----------------------+
| 1000       | 321         | 2014-08-27 14:41:02  |
+------------+-------------+----------------------+
| 7650       | 500         | 2014-08-27 17:41:02  |
我感兴趣的是按一天中的小时计算不同身份用户的数量

我尝试了以下方法,但当我按小时(日期)分组时,它无法保留以前输入的所有ID的记录

如果有任何问题,请告诉我。
提前感谢您的帮助。

我想您需要这样的帮助:

select date(time), hour(time),
       COUNT(distinct case when userId <> 1000 then userId end) as numSELFIDUsers
from usercarts
where date(time) = '2014-08-27'
group by date(time), hour(time)
order by 1, 2;
select d.dt, h.hr, COUNT(distinct case when userId <> 1000 then userId end) 
from (select distinct date(time) dt from usercarts where dt IN YOUR RANGE) d cross join
     (select 0 as hr union all select 1 union all select 2 union all select 3 union all . . .
      select 23
     ) h left join
     usercart uc
     on date(uc.time) = d.dt and hour(uc.time) = h.hr;

我不确定代码是否解决了这个问题。我想做的是按一天中的小时数统计不同的self-id用户的数量。我的代码不起作用,因为我按小时(日期)对它们进行分组;因此,返回的用户(在我的示例中为7650)再次被计算为self-id用户。我不想把它计算在内,因为用户已经提前完成了她的第一个购物车。希望这能澄清。@eagent\u learner313。编辑II应该正确计算它们。要用不同的措辞,您需要在它们第一次出现时对其进行计数。
select date(time), hour(time),
       COUNT(distinct case when userId <> 1000 then userId end) as numSELFIDUsers
from usercarts
where date(time) = '2014-08-27'
group by date(time), hour(time)
order by 1, 2;
select d.dt, h.hr, COUNT(distinct case when userId <> 1000 then userId end) 
from (select distinct date(time) dt from usercarts where dt IN YOUR RANGE) d cross join
     (select 0 as hr union all select 1 union all select 2 union all select 3 union all . . .
      select 23
     ) h left join
     usercart uc
     on date(uc.time) = d.dt and hour(uc.time) = h.hr;
select date(firsttime), hour(firsttime), count(*) as NumFirstUsers
from (select userId, min(time) as firsttime
      from usercarts
      where userid <> 1000
      group by userId
     ) u
group by date(firsttime), hour(firsttime)
order by 1, 2;