如果我在管理一个有费用的俱乐部(如咖啡、建筑维修、电力、蛋糕等)。每当俱乐部承担一笔开支时,就会在“所有实际支出”表中添加一项记录:
______________________ _______________________
|All Possible Expenses | |All Actual Expenses |
----------------------- -----------------------
| ID | | ID |
| Name of expense | | AllPossibleExpensesID |
----------------------- | Date Occured |
| Price |
-------------------------费用可以安排成基金(如‘2017年的费用’、‘食品费用’、‘商务会议费用’等)。
因此,资金表如下:
___________ _________________________________________
| Funds | | Funds LINK AllActualExpenses |
------------| |-----------------------------------------|
| Id | | FundID |
| Fund Name | | AllActualExpensesID that's in this fund |
------------ -----------------------------------------正如你所看到的,资金可以“重叠”,所以一笔费用可以是一些基金的一部分。
当钱存入俱乐部的金库时,这笔钱只能分配给某些基金,如:
_________________ _____________________________________
|Payments | | Payments LINK Funds |
-----------------| |------------------------------------|
| ID | | PaymentID |
| Date | | A FundID that this is allocated to |
| Amount Paid In | -------------------------------------
| Member ID |
-----------------|这看上去不错,但有一个问题,我一辈子都想不出该怎么做:
我如何显示每个基金有多少钱?请记住,一些钱可能分配给第一和第二基金,而另一些可能分配给基金二和THREE...so在现实中,有一个好的方法来显示多少钱在第一基金,多少钱在第二基金,有多少钱在第三基金?
问题是,如果这个程序简单地显示了每个基金中有多少钱,用户就会觉得俱乐部拥有的钱比它实际拥有的要多得多!
此外,还出现了以下问题:
我不知道,也许我忽略了一个显而易见的解决方案。这个模式以前解决过吗?提前谢谢你的任何想法。
发布于 2017-07-09 13:38:23
在“支付相关费用”中,您需要包括一个分配列。然后,您的查询如下:
Select flae.fundID, sum(ae.Price * flae.allocation) as price
from fundsLinkActualExpenses as flae join
ActualExpenses as ae
on flae.ExpensesID = flae.ExpensesID
group by flae.fundID;您将需要有触发器,以确保分配达到100%时,新的开支,资金和链接被添加。
发布于 2017-07-09 23:05:42
如果你有收入和支出,我认为你应该把它们分成一部分。
earning(id, amount)
primary key: id
spending(id,amount)
primary key: id
partial(earning_id,spending_id,amount)
primary key: earning_id,spending_id
foreign key: earning_id references earning(id)
foreign key: spending_id references spending(id)
constraints:
(sum(spending.amount) where spending.id=ARBITRARY_ID)
<= spending.amount where spending.id=ARBITRARY_ID
where ARBITRARY_ID is a valid id from spending
(sum(earning.amount) where earning.id=ARBITRARY_ID)
<= earning.amount where earning.id=ARBITRARY_ID
where ARBITRARY_ID is a valid id from earning所以假设
spendings
id amount
1 50
2 100
3 150
4 30
earnings
id amount
1 100
2 100
3 120
parts
earning_id spending_id amount
1 1 50
1 2 50
2 2 50
2 3 50
3 3 100
3 4 20所以我们有
payed by
spending earning remaining
1 1 0
2 1,2 0
3 2,3 0
4 3 10
used for
earning spending remaining
1 1,2 0
2 2,3 0
3 3,4 0所以我们知道钱是怎么用的。
分割到部分数量可以手动进行,但当然,它更好地使用一个子例程。子例程可以由触发器执行(这可能很复杂),也可以定期执行。
在您的设计中,收入将是您的付款,而支出将是您的开支。将金额除以的子例程必须考虑给付款项的数额。
如果把钱捐给基金以及单一的开支,那么两者都应该有一个共同的超级阶级。让我们称它为目标(毫无疑问会有更好的名称)。
所以我们有
target(id)
primary key(id)
expenses(id
primary key(id)
foreign key(id) references target(id)
funds(id)
primary key(id)
foreign key(id) references target(id)
money is not given to funds or expenses but to targets. https://stackoverflow.com/questions/44996578
复制相似问题