Forum Discussion
merging tables on two columns
Hi there,
First post, so apologies if this is super dumb - I was either searching for the wrong thing or being exceedingly dense in understanding what others had written! I have a large (1m row) data table and a small (few hundred row) configuration table and I am trying to merge them together, thus:
Table.Data
Date Monthly Production Unit
| 31/1/2017 | 1000 | CNC1 |
| 31/1/2017 | 2000 | CNC2 |
| 31/1/2017 | 3000 | CNC3 |
| 28/2/2017 | 1000 | CNC1 |
| 28/2/2017 | 2000 | CNC2 |
| 28/2/2017 | 3000 | CNC3 |
etc
Table.Configuration
Date Unit Type1Jobs Type2Jobs
| 1/1/2017 | CNC1 | 0.5 | 0.5 |
| 1/2/2017 | CNC1 | 0.6 | 0.4 |
| 1/1/2017 | CNC2 | 0.5 | 0.5 |
| 1/1/2017 | CNC3 | 0.5 | 0.5 |
| 1/2/2017 | CNC3 | 0 | 1 |
In other words, for the month of Jan, CNC1 did 50% of Type1 jobs and 50% of type 2 jobs, and then in Feb was re-configured to do 60% type 1 jobs and 40% type 2 jobs.
The outcome I want is effectively:
Date Monthly Production Unit Type1Work Type2Work
| 31/1/2017 | 1000 | CNC1 | 500 | 500 |
| 31/1/2017 | 2000 | CNC2 | 1000 | 1000 |
| 31/1/2017 | 3000 | CNC3 | 1500 | 1500 |
| 28/2/2017 | 1000 | CNC1 | 600 | 400 |
| 28/2/2017 | 2000 | CNC2 | 1000 | 1000 |
| 28/2/2017 | 3000 | CNC3 | 0 | 3000 |
So the monthly production is shared out based on the fraction of the type of work, per machine. The end result I want to see is type of work over time per machine, as those get grouped by site.
Now, I can do a simple left outer join to achieve a baseline configuration, but I cannot do a join on two columns without getting into between/range type thinking and that is beyond my brain at this time, and therefore any configuration change of the machine is lost.
I see answers sort of suggesting a 2nd date table somehow, but I am struggling to see the solution. I am sure this is super simple, what am I missing?
- Anonymous9 years ago
did you guys solve it with SQL after all?
If so, could I ask you to accept a solution for this post, so others can benefit from it anyway?
16 Replies
- vanessafvg
Community Champion
pxg638 why dont you create a new column, ie a key based on the combination of both the columns?
- pxg638Frequent Visitor
So I can create [Unit]&[Date] in both tables, that is simple enough, how would I then utilise that to create the change in allocation between jobtypes? Wouldn't a join on the new column simply leave lots of gaps where it didn't key up?
- vanessafvg
Community Champion
pxg638 ah ok, tell me a bit more about the change in allocation, its not clear to me from your post (i might be a bit slow this morning)