MySql Group 并按月汇总每个部门全年的销售额,加入多个表

Posted

技术标签:

【中文标题】MySql Group 并按月汇总每个部门全年的销售额,加入多个表【英文标题】:MySql Group and sum sales by by month for each departments across the year joining multiple tables 【发布时间】:2021-07-06 17:17:27 【问题描述】:

我一直在尝试实现以下目标,但无济于事。任何帮助将不胜感激。

部门 - 组织内的部门

id department_name
1 Process Consulting
2 Technical Consulting
3 Administrative Unit

服务 - 所提供的每个服务都映射到一个部门

id service_name department_id
1 Project management 1
2 Video production 2
3 Training 2
4 Facility Rental 3

事务 - 每个事务都映射到一个服务

id transaction_amount service_id transaction_date
1 2000 1 2021-02-04
2 3000 4 2021-01-01
3 1500 2 2021-03-14
4 3500 2 2021-03-20

我想要达到的结果

department_id department_name year_summary (Jan - Dec separated by comma)
1 Process Consulting 0,2000,0,0,0,0,0,0,0,0,0,0
2 Technical Consulting 0,0,5000,0,0,0,0,0,0,0,0,0
3 Administrative Unit 3000,0,0,0,0,0,0,0,0,0,0,0

【问题讨论】:

你用的是哪个版本的mysql @KaziMohammadAliNur 版本 10.3.28-MariaDB 【参考方案1】:

虽然你已经得到了答案,但我在这里分享另一个,因为它已经准备好了:

架构和插入语句:

 create table Departments(id int, department_name varchar(100));
 insert into Departments values(1,  'Process Consulting');
 insert into Departments values(2,  'Technical Consulting');
 insert into Departments values(3,  'Administrative Unit');  
 
 create table Services (id int, service_name varchar(100), department_id int);
 insert into Services values(1, 'Project management',   1);
 insert into Services values(2, 'Video production'  ,   2);
 insert into Services values(3, 'Training'      ,   2);
 insert into Services values(4, 'Facility Rental'   ,   3);
 
 create table Transactions(id int, transaction_amount int, service_id int, transaction_date date);
 insert into Transactions values(1, 2000,   1,  '2021-02-04');
 insert into Transactions values(2, 3000,   4,  '2021-01-01');
 insert into Transactions values(3, 1500,   2,  '2021-03-14');
 insert into Transactions values(4, 3500,   2,  '2021-03-20');

查询(使用公用表表达式和 group_concat() ):

 with Calendar as
   (
   SELECT 1 mon UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
   SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
   SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
   )
   , d as (select * from Calendar cross join Departments )
   , t as (select d.id,d.department_name ,month(transaction_date)mon, sum(transaction_amount)amountsum  from Departments d inner join Services s on d.id=s.department_id
 inner join Transactions t on s.id=t.service_id
 group by  d.id,d.department_name 
              )
              
 select d.id,d.department_name,group_concat(coalesce(amountsum,0) order by d.mon)  year_summary
 from d left join t on d.id=t.id and t.mon=d.mon
 group by d.id
              
              
              
   

输出:

id department_name year_summary
1 Process Consulting 0,2000,0,0,0,0,0,0,0,0,0,0
2 Technical Consulting 0,0,5000,0,0,0,0,0,0,0,0,0
3 Administrative Unit 3000,0,0,0,0,0,0,0,0,0,0,0

dbhere

【讨论】:

【参考方案2】:

您似乎希望在两个级别上进行条件聚合。第一个是按部门和月份的汇总:

select d.id, d.department_name, month(transaction_date) as mon,
       sum(t.transaction_amount) as amount
from departments d left join
     services s
     on s.department_id = d.id left join
     transactions t
     on t.service_id = s.id  
where t.transaction_date >= '2021-01-01' and
      t.transaction_date < '2022-01-01'   
group by d.id, d.department_name;

第二个然后以您想要的格式聚合:

select id, department_name,
       concat_ws(',',
                 max(case when mon = 1 then amount else 0 end),
                 max(case when mon = 2 then amount else 0 end),
                 . . .   -- fill in the rest of the months
                 max(case when mon = 12 then amount else 0 end)
                ) as year_summary
from (select d.id, d.department_name, month(transaction_date) as mon,
             sum(t.transaction_amount) as amount
      from departments d left join
           services s
           on s.department_id = d.id left join
           transactions t
           on t.service_id = s.id  
      where t.transaction_date >= '2021-01-01' and
            t.transaction_date < '2022-01-01'   
      group by d.id, d.department_name
     ) dm
group by id, department_name;

【讨论】:

以上是关于MySql Group 并按月汇总每个部门全年的销售额,加入多个表的主要内容,如果未能解决你的问题,请参考以下文章

mysql计算行并按月分组[重复]

MySQL之按月拆分主表并按月分表写入数据提高数据查询速度

MySQL按月汇总并运行总计[重复]

如何将MySQL表数据汇总半年?

如何在我的数据集中获得每个月的 MAX/MIN 函数?

按月汇总数据的分区行