Jam*_*mes 3 sql database-design
我正在计划/设计一个高尔夫俱乐部的MSSQL数据库。我有以下表格:
成员-标准联系信息成员资格类型-完整,初级,60多岁等。付款方式-现金,常规,支票等。
我苦苦挣扎的表格设计之一是付款。每年1月份的订阅金额都会更改,例如,在2010年,正式会员的全年订阅费用为1000英镑,因此,如果会员A是“正式会员”,他可以在10个月内分期或分期付款支付1000英镑。
这部分不是问题。我可以有一个带有PaymentID,MemberID,Date,PaymentAmount的付款表,这可以告诉我该会员迄今已支付了多少,还有多少尚未偿还。
对我来说,问题在于2011年1月,“正式会员”订阅额可能会增加到1100英镑,这在尝试进行计算时会造成问题。从理论上讲,我需要将2010年的付款存档,并在2011年重新开始–我不想这样做,因为我想显示该会员曾经进行过的每笔付款的历史记录。我欢迎针对这种情况下最有效的表设计的任何建议。
我相信您需要会员级别的可变订阅金额。例如,如果我报名参加并且您给我折扣以帮助您进行设计,则应向我收取的费用以及订阅日期位于会员级别,这样您便知道每月或一次收取多少费用。当我的订阅即将到期(滑动或静态)时,应向我收取基本费率,即新费率。此时,数学非常简单,因为您知道我注册时,到目前为止已支付的金额以及所欠的金额(按比例分配或滑动)。
这是一个基本示例:
Member
MemberId
Name
Address
Etc...
Product
ProductId
Name
Description
Price
Payment
PaymentId
SubscriptionId
Amount
DatePaid
Subscription
SubscriptionId
MemberId
ProductId
Price
StartDate
EndDate (if needed)
Run Code Online (Sandbox Code Playgroud)
未经测试的查询,但非常接近:
*-- how much do I owe?*
select m.Name, sum(s.Price) - sum(p.Amount) as Owes
from Member m
join Subscription s on m.MemberId = s.MemberId
join Payment p on s.SubscriptionId = p.SubscriptionId
where m.MemberId = 1
*-- how long have I been a member*
select datediff('year', min(StartDate), getdate()) as yrs,
datediff('month', min(StartDate), getdate()) as mths,
datediff('day', min(StartDate), getdate()) as dys
from Subscription
where MemberId = 1
*-- when does my subscription expire*
select max(EndDate) as ExpirationDate
from Subscription
where MemberId = 1
*-- list all payments by product*
select m.Name as MemberName,
pr.Name as ProductName,
pr.Price as ProductPrice,
s.Price as SubscriptionPrice,
p.Amount as AmountPaid
p.PaidDate,
from Member m
join Subscription s on m.MemberId = s.MemberId
join Payment p on s.SubscriptionId = p.SubscriptionId
join Product pr on s.ProductId = pr.ProductId
where MemberId = 1
Run Code Online (Sandbox Code Playgroud)