How could I create a query that provides a default transaction type for those transactions not categorized?
So assuming I have:
Aim:
BUT:
Example
Transactions
1 XXXX $100
2 YYY $100
3 ZZZ $100
Categories
1 aaa
2 bbb
3 ccc
4 PersonalDefault
Transaction-Categories (i.e. allocation)
transID catID %
1 1 100%
2 2 50%
Query Output I'm After
transTitle catTitle AllocatedAmount
xxxx aaaaa $100
yyyyy bbbbbb $50
yyyyy PersonalDefault $50
zzzzz PersonalDefault $100
This query will give you the desired result:
select a.transID, t.name as transname, a.catID, c.name as catname, t.amount * a.pc / 100 as amnt
from allocation a inner join transactions t on a.transID = t.id
inner join categories c on a.catID = c.id
union all
select a.transID, t.name as transname, 4 as catID, 'PersonalDefault' as catname, t.amount * (100 - a.pc) / 100 as amnt
from allocation a inner join transactions t on a.transID = t.id
where a.pc < 100
union all
select t.id as transID, t.name as transname, 4 as catID, 'PersonalDefault' as catname, t.amount as amnt
from transactions t
where t.id not in (select transID from allocation)
Check this SQL Fiddle (it is SQL Server, but should work in Access as well)