[英]Mysql sales table grouped by customer and displayed by month columns pivot query
I have a sales table with the following columns:我有一个包含以下列的销售表:
| Customer_Id | amount | date |
What would be the best way to group data by customer_id
, and displaying monthly total(SUM) of amount for each Customer_id (one row per Customer_id), on distinct month columns?按
customer_id
对数据进行分组并在不同的月份列上显示每个 Customer_id(每个 Customer_id 一行)的每月总金额(SUM)的最佳方法是什么?
The desired output would be something like:所需的输出类似于:
Customer |January | February | March | ....
Customer_id |SUM amount | SUM amount | SUM amount | ....
I believe in Sql this is called a pivot table.我相信在 Sql 中,这称为数据透视表。
¡Thanks!谢谢!
Let's say you have following table:假设您有下表:
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 |
+-------------+--------+------------+
You can do this using this query:您可以使用此查询执行此操作:
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;
And the output:和输出:
+-------------+------+------+------+------+------+------+------+------+------+------+------+------+
| 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 |
+-------------+------+------+------+------+------+------+------+------+------+------+------+------+
Try this:尝试这个:
SELECT `Customer_id` AS Customer,
SUM(`amount`) AS MONTHNAME(`date`)
FROM `sales`
GROUP BY YEAR(`date`), MONTH(`date`);
MySQL provides the WITH ROLLUP modifier for the GROUP BY clause. MySQL 为 GROUP BY 子句提供 WITH ROLLUP 修饰符。 Take a look at this.
看看这个。
... GROUP BY customer_id WITH ROLLUP
Hope it helps.希望能帮助到你。
Try this code.试试这个代码。
SELECT Customer_id
, SUM(amount), MONTHNAME(date) FROM sales_table
GROUP BY MONTH(date); SELECT
Customer_id
, SUM(amount), MONTHNAME(date) FROM sales_table
GROUP BY MONTH(date);
This query gets the desired result:此查询获得所需的结果:
SELECT Customer_id,
Sum(CASE WHEN MONTH(date) = 1 THEN amount END) AS Jan,
Sum(CASE WHEN MONTH(date) = 2 THEN amount END) AS Feb,
Sum(CASE WHEN MONTH(date) = 3 THEN amount END) AS Mar,
...
FROM table_name
GROUP BY Customer_id
Thanks to @shawnt00 for the heads up ;)感谢@shawnt00 的提醒;)
If you change each line like this in the answer from Piotr如果您在 Piotr 的答案中像这样更改每一行
sum(if(month(date) = 1, amount, 0)) AS Jan,
to到
sum(if((month(date) = 1 AND year(date) = 2017 ), amount,0)) AS Jan_17,
you can split the table when there is more than a year of data当数据超过一年时,您可以拆分表格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.