如何创建一个为未分类的交易提供默认交易类型的查询?
假设我有:
目的:
BUT:
实施例
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
答案 0 :(得分:1)
此查询将为您提供所需的结果:
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)
检查这个SQL Fiddle(它是SQL Server,但也应该在Access中工作)