按月分组并按 id mysql 分隔

Grouping by month and separate by id mysql

如何分组 table 从行作为月份到字段作为月份但按 id 分组,因此此查询按 2 个字段分组但行变成一个字段?

这是数据:

如果上面的图片没有出现下面是辅助信息:

我有 table 笔这样的交易:

Id| Id_product  | Month | Value |
 ________________________________
 1| 1           | 6     | 5     |
 2| 1           | 6     | 5     |
 3| 3           | 7     | 4     |
 4| 7           | 8     | 1     |
 5| 5           | 8     | 2     |
 6| 5           | 8     | 1     |
 7| 1           | 8     | 1     | 

我想用 2 个字段对所有数据进行分组,id product 但按月份分组,月份是一个字段,

    Id_product  | Month6       | Month7 | Month8       |
 ______________________________________________________
    1           | 10 (id1+id2) |        | 1 (id7)      |
    3           |              | 4(id3) |              |
    7           |              |        | 1 (id4)      |
    5           |              |        | 3 (id5 + id6)| 

一年有 12 个月..考虑到这是一个固定的假设, 以下可能是一个解决方案,

select id_product,
sum(month1),sum(month2),sum(month3),sum(month4),sum(month5),sum(month6),sum(month7),sum(month8),sum(month9),sum(month10),sum(month11),sum(month12)
from
    (select id_product,
    case when `month`= 1  then value else 0 end month1,
    case when `month`= 2  then value else 0 end month2, 
    case when `month`= 3  then value else 0 end month3, 
    case when `month`= 4  then value else 0 end month4, 
    case when `month`= 5  then value else 0 end month5, 
    case when `month`= 6  then value else 0 end month6, 
    case when `month`= 7  then value else 0 end month7, 
    case when `month`= 8  then value else 0 end month8, 
    case when `month`= 9  then value else 0 end month9, 
    case when `month`= 10  then value else 0 end month10,
    case when `month`= 10  then value else 0 end month11,
    case when `month`= 10  then value else 0 end month12
    from sfmonthquery) as T 
group by id_product

不需要子查询。
可以直接使用条件聚合:

SELECT id_product,
       SUM((Month = 1) * Value) Month1,
       SUM((Month = 2) * Value) Month2,
       SUM((Month = 3) * Value) Month3,
       SUM((Month = 4) * Value) Month4,
       SUM((Month = 5) * Value) Month5,
       SUM((Month = 6) * Value) Month6,
       SUM((Month = 7) * Value) Month7,
       SUM((Month = 8) * Value) Month8,
       SUM((Month = 9) * Value) Month9,
       SUM((Month = 10) * Value) Month10,
       SUM((Month = 11) * Value) Month11,
       SUM((Month = 12) * Value) Month12
FROM transaction
GROUP BY id_product

参见demo
结果:

> id_product | Month1 | Month2 | Month3 | Month4 | Month5 | Month6 | Month7 | Month8 | Month9 | Month10 | Month11 | Month12
> ---------: | -----: | -----: | -----: | -----: | -----: | -----: | -----: | -----: | -----: | ------: | ------: | ------:
>          1 |      0 |      0 |      0 |      0 |      0 |     10 |      0 |      1 |      0 |       0 |       0 |       0
>          3 |      0 |      0 |      0 |      0 |      0 |      0 |      4 |      0 |      0 |       0 |       0 |       0
>          5 |      0 |      0 |      0 |      0 |      0 |      0 |      0 |      3 |      0 |       0 |       0 |       0
>          7 |      0 |      0 |      0 |      0 |      0 |      0 |      0 |      1 |      0 |       0 |       0 |       0