【发布时间】:2019-05-14 00:38:22
【问题描述】:
所以我在这里想要做的是,我试图计算一段时间内重复用户(下单以上订单的用户)的数量,让它成为月日或年,这种情况这是几个月
我目前正在运行 mysql mariadb,我几乎是 mysql 的初学者,我尝试了多个子查询,但到目前为止都失败了
这是我迄今为止尝试过的..
这将返回所有没有订购计数条件的用户数
由于人们要求提供样本数据,以下是目前数据的样子:
Order_Creation_Date - User_ID - Order_ID
2019-01-01 123 1
2019-01-01 123 2
2019-01-01 231 3
2019-01-01 231 4
这是我用来获取结果的查询,但它不断返回当月的用户总数
select month(o.created_at)month,
year(o.created_at)year,
count(distinct o.user_uuid) from orders o
group by month(o.created_at)
having count(*)>1
这会将用户数返回为 1 ..
select month(o.created_at)month,
year(o.created_at)year,
(select count(distinct ord.user_uuid) from orders ord
where ord.user_uuid = o.user_uuid
group by ord.user_uuid
having count(*)>1) from orders o
group by month(o.created_at)
预期结果将来自上面的示例数据
Month Count of repeat users
1 2
【问题讨论】:
-
样本数据和期望的结果会有所帮助。
-
我认为您需要按年和月分组。正如@Gordon 所说,样本数据会有很大帮助。当您说您发布的查询返回什么时,我不知道您的意思,因为您没有显示它们返回的内容。你只要告诉我们。
-
没有样本数据集的预期结果是愚蠢的。你可以看到,对吧?我们
-
大家好,添加了样本数据和样本数据的预期结果