【问题标题】:Grouping by month and separate by id mysql按月分组并按 id mysql 分隔
【发布时间】:2020-10-07 02:37:28
【问题描述】:

如何将表从行按月分组到字段按月分组但按id分组,所以这个查询是按2个字段分组但行变成一个字段?

这是数据:

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

我有这样的表事务:

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 产品但按月份分组,并且月份是一个字段,

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

【问题讨论】:

  • 你必须有一个交叉表格式吗?
  • @PM77-1 ,假设它是一个表。真正的桌子很复杂。所以不是这个事务的另一个表,我们不需要它。

标签: mysql row grouping field


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    一年有 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
    

    【讨论】:

    • 太好了,感谢您帮助回答我的问题,就这么简单.. :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多