【发布时间】:2022-01-01 00:19:37
【问题描述】:
我有一个正常的周转表,有 3 列“客户”、“年份”、“金额” (例如)
| customer | year | amount |
|---|---|---|
| anton | 2020 | $5 |
| paul | 2019 | $12 |
| anton | 2021 | $5 |
| paul | 2019 | $10 |
| felicia | 2021 | $5 |
| anton | 2019 | $12 |
| felipe | 2019 | $12 |
我有以下 mysql 查询
SELECT `customer` , SUM(`amount`) as summ FROM `customer`.`accountsales` WHERE `amount`> 0 GROUP BY `customer` ORDER BY summ DESC ;
这个交易给了我一个很好的帕累托表,每个客户的销售额按降序排列
| name | sales all years |
|---|---|
| customer1 | sum of all transactions of customer1 |
| customer2 | sum of all transactions of customer2 |
| customer3 | sum of all transactions of customer3 |
到目前为止一切顺利,我想更进一步,我想创建下表
| Name | Sales all years | Sales 2021 | Sales2020 | Sales2019 |
|---|---|---|---|---|
| customer1 | sum1 | sum2021 from customer1 | sum2020 from customer1 | sum2019 from customer1 |
| customer2 | sum2 | sum2021 from customer2 | sum2020 from customer2 | sum2019 from customer2 |
| customer3 | sum3 | sum2021 from customer3 | sum2020 from customer3 | sum2019 from customer3 |
但我只想在一个事务中完成,因为初始表非常大。
谁能给个提示?
附言请随意编辑标题,因为我今天不太受启发
【问题讨论】:
-
到目前为止您尝试过什么?你被困在哪里了?
标签: mysql