【问题标题】:Getting wrong results when trying to get the sum and averages for each month尝试获取每个月的总和和平均值时得到错误的结果
【发布时间】:2020-05-20 10:57:24
【问题描述】:

我有一个orders 表。我需要得到每个月的总数 order_sum 以及每个月的日平均值。

例如,我们可以看到Jan 1 的总和为57.86Jan 2 的总和为44.61。那么January 月份的日平均值将是51.24。但是我的查询让我得到错误的结果 daily_average (它显示 28.93)

这是我的orders

+----+-----------+---------------------+-------------------------------+
| id | order_sum | created_at          |                               |
+----+-----------+---------------------+-------------------------------+
| 1  | 47.36     | 2020-01-01 08:02:17 |                               |
| 2  | 10.50     | 2020-01-01 11:19:00 | <-- total for Jan 1 is 57.86  |
|    |           |                     |                               |
| 3  | 5.04      | 2020-01-02 09:21:43 |                               |
| 4  | 39.57     | 2020-01-02 12:59:28 | <-- total for Jan 2 is 44.61  |
|    |           |                     |                               |
| 5  | 63.01     | 2020-02-01 17:04:50 |                               |
| 6  | 7.00      | 2020-02-01 19:36:02 | <-- total for Feb 1 is 70.01  |
|    |           |                     |                               |
| 7  | 3.12      | 2020-02-02 07:20:11 |                               |
| 8  | 119.98    | 2020-02-02 23:51:51 | <-- total for Feb 2 is 123.10 |
+----+-----------+---------------------+-------------------------------+

这是我的查询(错误)

select
  MONTH(created_at) as month,
  sum(order_sum) as order_sum,
  second_table.daily_average as daily_average
from
  orders
inner join (
  select
    month(created_at) as month,
    avg(order_sum) as daily_average
  from
    orders
  group by date(created_at)
) as second_table on MONTH(created_at) = second_table.month
group by
  MONTH(created_at);

这是我想要得到的结果

+-------+-----------+---------------+
| month | order_sum | daily_average |
+-------+-----------+---------------+
| 1     | 102.47    | 51.24         |
| 2     | 193.11    | 96.56         |
+-------+-----------+---------------+

这是一个示例架构...

CREATE TABLE orders(
   id         INTEGER  NOT NULL PRIMARY KEY,
   order_sum  NUMERIC(11,2) NOT NULL,
   created_at VARCHAR(21) NOT NULL
);

INSERT INTO orders(id, order_sum, created_at) VALUES 
(1, 47.36, '2020-01-01 08:02:17'),
(2, 10.50, '2020-01-01 11:19:00'),
(3, 5.04, '2020-01-02 09:21:43'),
(4, 39.57, '2020-01-02 12:59:28'),

(5, 63.01, '2020-02-01 17:04:50'),
(6, 7.00, '2020-02-01 19:36:02'),
(7, 3.12, '2020-02-02 07:20:11'),
(8, 119.98, '2020-02-02 23:51:51');

... 和小提琴一样: http://sqlfiddle.com/#!9/36ae43/7

【问题讨论】:

  • 如果当天没有销售,日均值是多少?
  • @GordonLinoff 那么当天的总数将为 0。该月的每日平均值将为 ... (10 + 5 ... + 0) / count
  • 您正在按不同的粒度进行分组。在您的内部查询中,您按天分组,而在您的外部查询中,您按月分组。正如@GordonLinoff 下面所说,如果您按月分组,则应始终包括年份。

标签: mysql sql database


【解决方案1】:

在处理月份时,您应该始终包含year(),除非您打算省略它。

我想你想要:

select year(created_at) as year, month(created_at) as month,
       sum(order_sum) as order_sum,
       sum(order_sum) / count(distinct date(created_at)) as daily_average
from orders o
group by year(created_at), month(created_at);

【讨论】:

  • 谢谢 Gordon,看来这正是我所需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 2021-10-25
相关资源
最近更新 更多