Mysql 计算各门店每月创建的平均潜在客户数
我想计算每个商店每月创建的潜在客户(行)的平均数量 模式和输入:Mysql 计算各门店每月创建的平均潜在客户数,mysql,sql,Mysql,Sql,我想计算每个商店每月创建的潜在客户(行)的平均数量 模式和输入: CREATE TABLE leads (`id` int, `store_id` int, `created_at` datetime) ; INSERT INTO leads (`id`, `store_id`, `created_at`) VALUES (5211, 1, '2019-09-13 23:29:29'), (5212, 1, '2019-08-13 21:29:29'),
CREATE TABLE leads
(`id` int, `store_id` int, `created_at` datetime)
;
INSERT INTO leads
(`id`, `store_id`, `created_at`)
VALUES
(5211, 1, '2019-09-13 23:29:29'),
(5212, 1, '2019-08-13 21:29:29'),
(5781, 1, '2019-08-16 21:29:29'),
(3349, 5, '2019-10-16 23:29:29'),
(3344, 5, '2019-10-16 23:29:29'),
(6291, 8, '2019-08-14 21:29:29'),
(6292, 8, '2019-08-14 22:29:29'),
(6299, 8, '2019-08-14 11:29:29'),
(7799, 8, '2019-10-16 23:29:29'),
(9898, 8, '2019-08-13 23:29:29'),
(7791, 8, '2019-10-16 23:29:29'),
(7792, 8, '2019-10-16 23:29:29'),
(7793, 8, '2019-10-16 23:29:29'),
(7794, 8, '2019-10-16 23:29:29'),
(7795, 8, '2019-10-16 23:29:29')
;
average_leads_per_month_per_store, month_name
3, 2018-08
1, 2018-09
4, 2018-10
SELECT COUNT(id) AS leads_count, DATE_FORMAT(created_at, "%Y-%m") month_name, store_id, created_at
FROM leads
GROUP BY month_name, store_id
ORDER BY month_name, store_id;
| leads_count | month_name | store_id | created_at |
|-------------|------------|----------|----------------------|
| 2 | 2019-08 | 1 | 2019-08-13T21:29:29Z |
| 4 | 2019-08 | 8 | 2019-08-14T21:29:29Z |
| 1 | 2019-09 | 1 | 2019-09-13T23:29:29Z |
| 2 | 2019-10 | 5 | 2019-10-16T23:29:29Z |
| 6 | 2019-10 | 8 | 2019-10-16T23:29:29Z |
所需输出:
CREATE TABLE leads
(`id` int, `store_id` int, `created_at` datetime)
;
INSERT INTO leads
(`id`, `store_id`, `created_at`)
VALUES
(5211, 1, '2019-09-13 23:29:29'),
(5212, 1, '2019-08-13 21:29:29'),
(5781, 1, '2019-08-16 21:29:29'),
(3349, 5, '2019-10-16 23:29:29'),
(3344, 5, '2019-10-16 23:29:29'),
(6291, 8, '2019-08-14 21:29:29'),
(6292, 8, '2019-08-14 22:29:29'),
(6299, 8, '2019-08-14 11:29:29'),
(7799, 8, '2019-10-16 23:29:29'),
(9898, 8, '2019-08-13 23:29:29'),
(7791, 8, '2019-10-16 23:29:29'),
(7792, 8, '2019-10-16 23:29:29'),
(7793, 8, '2019-10-16 23:29:29'),
(7794, 8, '2019-10-16 23:29:29'),
(7795, 8, '2019-10-16 23:29:29')
;
average_leads_per_month_per_store, month_name
3, 2018-08
1, 2018-09
4, 2018-10
SELECT COUNT(id) AS leads_count, DATE_FORMAT(created_at, "%Y-%m") month_name, store_id, created_at
FROM leads
GROUP BY month_name, store_id
ORDER BY month_name, store_id;
| leads_count | month_name | store_id | created_at |
|-------------|------------|----------|----------------------|
| 2 | 2019-08 | 1 | 2019-08-13T21:29:29Z |
| 4 | 2019-08 | 8 | 2019-08-14T21:29:29Z |
| 1 | 2019-09 | 1 | 2019-09-13T23:29:29Z |
| 2 | 2019-10 | 5 | 2019-10-16T23:29:29Z |
| 6 | 2019-10 | 8 | 2019-10-16T23:29:29Z |
我尝试过的:
CREATE TABLE leads
(`id` int, `store_id` int, `created_at` datetime)
;
INSERT INTO leads
(`id`, `store_id`, `created_at`)
VALUES
(5211, 1, '2019-09-13 23:29:29'),
(5212, 1, '2019-08-13 21:29:29'),
(5781, 1, '2019-08-16 21:29:29'),
(3349, 5, '2019-10-16 23:29:29'),
(3344, 5, '2019-10-16 23:29:29'),
(6291, 8, '2019-08-14 21:29:29'),
(6292, 8, '2019-08-14 22:29:29'),
(6299, 8, '2019-08-14 11:29:29'),
(7799, 8, '2019-10-16 23:29:29'),
(9898, 8, '2019-08-13 23:29:29'),
(7791, 8, '2019-10-16 23:29:29'),
(7792, 8, '2019-10-16 23:29:29'),
(7793, 8, '2019-10-16 23:29:29'),
(7794, 8, '2019-10-16 23:29:29'),
(7795, 8, '2019-10-16 23:29:29')
;
average_leads_per_month_per_store, month_name
3, 2018-08
1, 2018-09
4, 2018-10
SELECT COUNT(id) AS leads_count, DATE_FORMAT(created_at, "%Y-%m") month_name, store_id, created_at
FROM leads
GROUP BY month_name, store_id
ORDER BY month_name, store_id;
| leads_count | month_name | store_id | created_at |
|-------------|------------|----------|----------------------|
| 2 | 2019-08 | 1 | 2019-08-13T21:29:29Z |
| 4 | 2019-08 | 8 | 2019-08-14T21:29:29Z |
| 1 | 2019-09 | 1 | 2019-09-13T23:29:29Z |
| 2 | 2019-10 | 5 | 2019-10-16T23:29:29Z |
| 6 | 2019-10 | 8 | 2019-10-16T23:29:29Z |
这是按年度、月份和门店分组的,现在我需要计算各个门店每月的平均值
据我所知,这将为您提供平均值
SELECT COUNT(id)*1.0/(1.0*count(distinct store_id)) AS leads_count, DATE_FORMAT(created_at, "%Y-%m") month_name
FROM leads T
GROUP BY month_name
ORDER BY month_name;
根据你的结果, 在
2019-08
月,您有2+4个Lead\u count
每个1和8
store\u id,因此,您有(6/2=3)
在2019-09年month,每个1
store\u id您有1个Lead\u count
,因此,您有(1/1=1)
在2019-10
月,您有2+6个Lead\u count
每个5和8
store\u id,因此,您有(8/2=4)
因此,您需要计算Lead\u count
然后划分store\u id
的总数
你可以通过这种方式实现它
SELECT COUNT(id) / Count(Distinct (store_id)) AS average_leads_per_month_per_store, DATE_FORMAT(created_at, "%Y-%m") month_name
FROM leads T
GROUP BY month_name
ORDER BY month_name;
实时演示。MySQL中最简单的语法似乎是:
SELECT DATE_FORMAT(created_at, '%Y-%m') as month_name,
COUNT(*) / COUNT(DISTINCT store_id) as avg_per_month
FROM leads T
GROUP BY month_name
ORDER BY month_name;
编辑:
如果您需要统计没有潜在客户的门店,那么您需要所有门店的一些来源。您大概有一个存储
表:
SELECT DATE_FORMAT(l.created_at, '%Y-%m') as month_name,
COUNT(*) / s.num_stores as avg_per_month
FROM leads l. CROSS JOIN
(SELECT COUNT(*) as num_stores FROM stores) s
GROUP BY l.month_name, s.num_stores
ORDER BY month_name;
这回答了你的问题吗?如果您需要帮助,请告诉我@Andrew.MastonNote,DISTINCT不是一个功能,如果某个商店在某个特定月份没有潜在客户,该怎么办?它是被忽略还是被计为0
?它应该被计为@GordonLinoff。