【发布时间】:2021-10-20 08:02:00
【问题描述】:
我有一个表名billing,我的表数据如下所示
| id | site_name | billing_date | amount |
|---|---|---|---|
| 1 | abc | 2021-01-15 | 100 |
| 2 | abc | 2021-02-15 | 80 |
| 3 | abc | 2021-03-15 | 120 |
| 4 | abc | 2021-04-15 | 110 |
| 5 | abc | 2021-05-15 | 105 |
| 6 | abc | 2021-06-15 | 90 |
| 7 | abc | 2021-07-15 | 106 |
| 8 | abc | 2021-08-15 | 70 |
| 9 | xyz | 2021-01-15 | 100 |
| 10 | xyz | 2021-02-15 | 90 |
| 11 | xyz | 2021-03-15 | 200 |
| 12 | xyz | 2021-04-15 | 300 |
我的预期结果会是这样的数据
| site_name | Year | January | February | March | Arpil | May | June | July | August | September | October | November | December |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| abc | 2021 | 100 | 80 | 120 | 110 | 105 | 90 | 106 | 70 | 0 | 0 | 0 | 0 |
| xyz | 2021 | 100 | 90 | 200 | 300 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
另外site_name,abc月份January只有一条记录,February和其他月份一样只有一条记录。我已经尝试了以下代码,但没有得到预期的输出
select site_name, YEAR(billing_date) as YearName, monthname(billing_date) as monthName, amount
from bills
group by site_name, YEAR(billing_date),monthname(billing_date),amount
order by site_name;
请对如何获得我的预期输出提出任何建议
【问题讨论】:
-
@jarlh,非常感谢。我会按照您的建议尝试这种方式