I've been trying to achieve the following to no avail. Any help will be appreciated.
Departments - Departments within an organization
| id | department_name |
|---|---|
| 1 | Process Consulting |
| 2 | Technical Consulting |
| 3 | Administrative Unit |
Services - Each service rendered is mapped to a department
| id | service_name | department_id |
|---|---|---|
| 1 | Project management | 1 |
| 2 | Video production | 2 |
| 3 | Training | 2 |
| 4 | Facility Rental | 3 |
Transactions - Each transaction is mapped to a service
| 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 |
The result I would like to achieve
| 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 |
You seem to want conditional aggregation at two levels. The first is a summary by department and month:
The second then aggregates that in the format you want: