简体   繁体   English

无法同时获得月份的总和以及每个月每天的平均总和

[英]Can't get both the sum of month and also the average sum per day of each month

I have a table orders and I need to get the sum of order_sum for each month.我有一个表orders ,我需要获得每个月的order_sum总和。 That is easy using group.使用组很容易。 But then I also need to get the average sum per day for each month.但是我还需要获得每个月每天的平均金额。 I am using joins so I can sort the results later.我正在使用连接,以便稍后对结果进行排序。 (I'm using MySQL version 5.6) (我使用的是 MySQL 5.6 版)

I am getting an error:我收到一个错误:

Unknown column 'orders.month' in 'on clause' “on 子句”中的未知列“orders.month”

Here is my orders table:这是我的orders表:

+----+-----------+---------------------+
| id | order_sum | created_at          |
+----+-----------+---------------------+
| 1  | 25.13     | 2020-01-05 08:02:17 |
| 2  | 1.01      | 2020-01-25 20:13:20 |
| 3  | 5.04      | 2020-01-29 16:11:56 |
| 4  | 39.57     | 2020-02-17 15:14:09 |
| 5  | 63.01     | 2020-02-28 17:13:55 |
| 6  | 7         | 2020-03-02 07:10:02 |
+----+-----------+---------------------+

Here is my query:这是我的查询:

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

This is the result that I am trying to get这是我想要得到的结果

+-------+-----------+-----------------+
| month | order_sum | average_per_day |
+-------+-----------+-----------------+
| 1     | 31.18     | 10.39           |
| 2     | 102.58    | 51.29           |
| 3     | 7         | 7               |
+-------+-----------+-----------------+

I know that I can easily get the average per day using this simple query but I'm unable to merge this into one query using inner join...我知道我可以使用这个简单的查询轻松获得每天的平均值,但我无法使用内部联接将其合并为一个查询...

select month(created_at) as month, avg(order_sum) as average_per_day from orders group by month

And here is the schema这是架构

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, 25.13, '2020-01-05 08:02:17'),
(2, 1.01, '2020-01-25 20:13:20'),
(3, 5.04, '2020-01-29 16:11:56'),
(4, 39.57, '2020-02-17 15:14:09'),
(5, 63.01, '2020-02-28 17:13:55'),
(6, 7.00, '2020-03-02 07:10:02');

Here is a fiddle with schema: http://sqlfiddle.com/#!9/b6b15/10这是架构的小提琴: http ://sqlfiddle.com/#!9/ b6b15/10

You can simply try like:您可以简单地尝试:

SELECT month(created_at) AS month
    ,sum(order_sum) AS Order_Sum
    ,avg(order_sum) AS average_per_day
FROM orders
GROUP BY month

You need to refer to MONTH(created_at) in the GROUP BY and the JOIN ON (NOT the alias) and also group your second_table .您需要在GROUP BYJOIN ON (不是别名)中引用MONTH(created_at) ) 并将您的second_table分组。 This works for me:这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM