【问题标题】:Calculating average leads created per month, across stores计算跨商店每月创建的平均潜在客户
【发布时间】:2020-01-12 05:17:13
【问题描述】:

我想计算每个商店每月创建的平均潜在客户数量(行)。

架构和输入:

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 |

这按年月和商店分组,现在我需要计算商店每个月的平均值。

http://sqlfiddle.com/#!9/034661/14

【问题讨论】:

  • 如果商店在特定月份没有潜在客户怎么办?是忽略还是算作0
  • 应该算@GordonLinoff。

标签: mysql sql


【解决方案1】:

据我了解,这会给你平均水平

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;

【讨论】:

    【解决方案2】:

    根据你的结果,

    2019-08 月,您拥有2 + 4 leads_count 每个1 & 8 store_id,因此,您拥有(6/2 = 3)

    2019-09 月,您拥有1 leads_count1 store_id,因此,您拥有(1/1 = 1)

    2019-10 月,您拥有2 + 6 leads_count 每个5 & 8 store_id,因此,您拥有(8 / 2 = 4)

    因此,您需要计算leads_count 然后除以store_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;
    

    现场演示here

    【讨论】:

    • 这能回答你的问题吗?如果您需要任何帮助,请告诉我@Andrew.Maston
    • 请注意,DISTINCT 不是函数
    【解决方案3】:

    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;
    

    编辑:

    如果您需要统计有 no 潜在客户的商店,那么您需要所有商店的一些来源。想必你有一个stores 表:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多