adhoustonj
Member
- Local time
- Today, 10:18
- Joined
- Sep 23, 2022
- Messages
- 193
Hello AWF,
I have a task table like below, and I am trying to write a query that returns the 2nd table below.
I need to sum up the task_secs by station and have a column for each model. The by model sum includes each task_secs where there is no model.
So for station 1 model 1 - it will sum the task ID's 1,2,3,4
for station 1 model 2 - it will sum task ID's 1,3,5
I'm scratching my head a bit while I thought this would be simpler.
I was using a pivot to sum it up for each model - null, 1,2,3 - but I then need to sum up null + model 1, null + model 2, null + model 3, etc.
There also can be any number of model variants in this table, so I liked that the pivot returned dynamic number of columns, but I am stuck.
If anyone could help that would be amazing.
I have a task table like below, and I am trying to write a query that returns the 2nd table below.
I need to sum up the task_secs by station and have a column for each model. The by model sum includes each task_secs where there is no model.
So for station 1 model 1 - it will sum the task ID's 1,2,3,4
for station 1 model 2 - it will sum task ID's 1,3,5
I'm scratching my head a bit while I thought this would be simpler.
I was using a pivot to sum it up for each model - null, 1,2,3 - but I then need to sum up null + model 1, null + model 2, null + model 3, etc.
There also can be any number of model variants in this table, so I liked that the pivot returned dynamic number of columns, but I am stuck.
If anyone could help that would be amazing.
Task_ID | Model | Station_ID | Task_secs |
---|---|---|---|
1 | 1 | 5 | |
2 | 1 | 1 | 8 |
3 | 1 | 10 | |
4 | 1 | 1 | 16 |
5 | 2 | 1 | 81 |
6 | 3 | 1 | 20 |
7 | 1 | 2 | 7 |
8 | 2 | 19 | |
9 | 2 | 2 | 2 |
Station_ID | Model_1 | Model_2 | Model_3 |
---|---|---|---|
1 | 39 | 96 | 35 |
2 | 26 | 21 |