I have budgets similar to example below, where:
- some budgets are person specific,
- some are for the entire team, and
- a mixture where some are part allocated to a specific person while the rest is for the rest of the team.
I can link Budgets to Summary_of_spend, but what I need to do is always link on year and budget type, but only on the person if one exists in the budget. Where there is no person in the budget, just default to sum together the amounts for that group.
How would I create a join that does for that?
(Sorry, not come across anything like this before and not sure what it’s call, let alone how to solve it. I’m using SQLite by the way.)
Budgets
YEAR, TYPE, PERSON, AMOUNT
2010, Sales, Bob, 1000
2010, Sales, John, 1000
2010, Sales, Fred, 1000
2010, Marketing, Null, 5000
2010, Special, Null, 3000
2010, Special, Bob, 1000
2011, Sales_budget, Bob, 1100
2011, Sales_budget, John, 1100
2011, Sales_budget, Fred, 1100
2011, Marketing, Null, 6000
2010, Special, Null, 3000
2010, Special, Bob, 1000
Spend
DATE, BUDGET, PERSON, AMOUNT
01/01/2010, Sales, Bob, 50
02/01/2010, Marketing, Bob, 100
...
Summary_of_spend
YEAR, BUDGET, PERSON, AMOUNT
2010, Sales, Bob, 950
2010, Sales, John, 888
2010, Marketing, Bob, 500
2010, Marketing, John, 500
2010, Marketing, Fred, 500
2010, Special, Bob, 333
2010, Special, John, 222
2010, Special, Fred, 222
...
Budget_and_summary_of_spend
YEAR, BUDGET_TYPE, PERSON, BUDGET_AMOUNT, SPEND_AMOUNT
2010, Sales, Bob, 1000, 950
2010, Sales, John, 1000, 888
2010, Sales, Fred, 1000, 0
2010, Marketing, Null, 5000, 1500
2010, Special, Null, 3000, 444
2010, Special, Bob, 1000, 333
...
You might want to try this:
You can join tables based on any expression. In this case I joined them in such a way that if person is not null the other side must have record for this person and if it is null I substitute some string that will not be found among persons. This is of course weak spot, and if it bothers you you can change join to a combination of and’s and or’s.
If you need records from budget where no money has been spent, change “inner join” to “left join”.