【发布时间】:2016-10-14 17:59:29
【问题描述】:
我有一个包含以下列的销售表:
mysql> select * from sales;
+-------------+--------+------------+
| customer_id | amount | date |
+-------------+--------+------------+
| 1 | 12 | 2015-01-01 |
| 1 | 1 | 2015-01-02 |
| 1 | 663 | 2015-02-12 |
| 2 | 22 | 2015-01-03 |
| 2 | 21 | 2015-02-12 |
| 2 | 11 | 2015-02-12 |
| 2 | 9 | 2015-04-12 |
+-------------+--------+------------+
我可以使用这个查询来接近我想要的:
SELECT
customer_id,
sum(if(month(date) = 1, amount, 0)) AS Jan,
sum(if(month(date) = 2, amount, 0)) AS Feb,
sum(if(month(date) = 3, amount, 0)) AS Mar,
sum(if(month(date) = 4, amount, 0)) AS Apr,
sum(if(month(date) = 5, amount, 0)) AS May,
sum(if(month(date) = 6, amount, 0)) AS Jun,
sum(if(month(date) = 7, amount, 0)) AS Jul,
sum(if(month(date) = 8, amount, 0)) AS Aug,
sum(if(month(date) = 9, amount, 0)) AS Sep,
sum(if(month(date) = 10, amount, 0)) AS Oct,
sum(if(month(date) = 11, amount, 0)) AS Nov,
sum(if(month(date) = 12, amount, 0)) AS `Dec`
FROM sales
GROUP BY customer_id;
所需的输出格式:
+-------------+------+------+------+------+------+------+------+------+------+------+------+------+
| customer_id | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
+-------------+------+------+------+------+------+------+------+------+------+------+------+------+
| 1 | 13 | 663 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 22 | 32 | 0 | 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+-------------+------+------+------+------+------+------+------+------+------+------+------+------+
但是我想要每月的平均值,当我将 sum 更改为 avg 时,我没有得到平均值。
SELECT
customer_id,
avg(if(month(date) = 1, amount, 0)) AS Jan,
avg(if(month(date) = 2, amount, 0)) AS Feb,
avg(if(month(date) = 3, amount, 0)) AS Mar,
avg(if(month(date) = 4, amount, 0)) AS Apr,
avg(if(month(date) = 5, amount, 0)) AS May,
avg(if(month(date) = 6, amount, 0)) AS Jun,
avg(if(month(date) = 7, amount, 0)) AS Jul,
avg(if(month(date) = 8, amount, 0)) AS Aug,
avg(if(month(date) = 9, amount, 0)) AS Sep,
avg(if(month(date) = 10, amount, 0)) AS Oct,
avg(if(month(date) = 11, amount, 0)) AS Nov,
avg(if(month(date) = 12, amount, 0)) AS `Dec`
FROM sales
GROUP BY customer_id;
产出(不是每月平均):
+-------------+--------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| customer_id | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
+-------------+--------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 1 | 4.3333 | 221.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 2 | 5.5000 | 8.0000 | 0.0000 | 2.2500 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
+-------------+--------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
我有点想弄清楚发生了什么,有人提供一些建议吗?谢谢
CREATE TABLE `sales` (
`customer_id` int(11) NOT NULL,
`amount` int(11) DEFAULT NULL,
`date` date DEFAULT NULL
)
INSERT INTO `sales` VALUES (1,12,'2015-01-01'),(1,1,'2015-01-02'),(1,663,'2015-02-12'),(2,22,'2015-01-03'),(2,21,'2015-02-12'),(2,11,'2015-02-12'),(2,9,'2015-04-12');
【问题讨论】:
-
尝试在 avg 中将 0 更改为 NULL:
avg(if(month(date) = 1, amount, NULL)) -
根据其他现有记录在一个月内插入任意多个
0,您会偏向您的平均值。也许更好group by customer_id, MONTH(date)? -
考虑处理表示层/应用程序级代码中的数据显示问题(如果可用)