我正在尝试捕获在我们网站上注册然后在同一天购买的用户数量 . 在我的查询上有点迷失 . 它返回结果,但经过仔细检查(这些是乐福鞋),结果不正确 .
我有2个表: customer
和 purchase
. 两个表都有 customer_id
和 date
(对于customer表,日期为"date_created",购买表为"date_modified") .
基本上我想得到每天在 customer_id
表中创建的user_ids的聚合计数,然后购买当天出现在 purchase
表中 . *注意:有's also a condition where I don' t想要包含来自customer_id的购买1. customer_id 1是一个测试帐户,有时用于在网站上进行测试购买 .
这是我试过的:
select
date_format(c.`date_created`, '%Y-%m-%d') as day_date
,x.same_day_purchase
from `customer` c
left join (
select
p.`customer_id`
,p.`date_modified`
,sum(if(p.`date_modified` >= cp.`date_created`,1,0)) as same_day_purchase
from purchase p
inner join `customer` cp on p.`customer_id` = cp.`customer_id` and p.`customer_id`<>1
group by cp.`date_created`
) x on c.`customer_id` = x.`customer_id` and x.`customer_id`<>1
where (c.`date_created` >= '2011/09/01' and c.`date_created` < '2014/05/14')
group by day_date
(所以我要找的结果是:
2011/09/01 - 3
2011/09/02 - 16
等等
因此,在2011年9月1日,3位用户来到我们的网站并注册,然后在当天购买了一些东西 . 第二天我们有16人新注册的客户购买产品,等等....)
谢谢!
2 回答
这是您的出发点:
你可以试试这个