我在数据库中有两个表。
MembershipInstallments(ProjectId, AllotmentId, InstallmentId, EntryDate,AmountPaid)
Memberships(ProjectId, AllotmentId,ClientId,ClientName, RegistrationNo)
[我想选择客户在一年中按月支付的款项。还有那年开始之前支付的金额。我想要的结果集是
让我们说2019年
ClientName, RegistrationNumbers, PreviouslyPaidAmount(i-e all amount paid in 2018), AmountPaidInJuly 2019, AmountPaidInAugust2019. ..... AmountPaidIn Jun2020
EntryDate
表中的MembershipInstallments
是月份,>
我通过跟踪方法尝试过,但是对于大型数据集来说花费的时间太长。
SELECT ClientName,RegistrationNo, (SELECT ISNULL(SUM(AmountPaid),0) FROM MembershipInstallments WHERE ProjectId=Membership.ProjectId AND AllotmentId=Membership.AllotmentId AND EntryDate<@fromDate) ASPreviouslyPaid, (SELECT ISNULL (SUM(AmountPaid),0 FROM MembershipInstallments WHERE ProjectId=Membership.ProjectId AND AllotmentId=Memberhsip.AllotmentId AND (MONTH(EntryDate) = 7) AND (YEAR(EntryDate) = YEAR(@fromDate)) AS JulyPayment FROM Memberships WHERE ProjectId=@projectId
任何帮助将不胜感激。
我在数据库中有两个表。 MembershipInstallments(ProjectId,AllotmentId,InstallmentId,EntryDate,AmountPaid)Memberships(ProjectId,AllotmentId,ClientId,ClientName,RegistrationNo),我想...
Scalar子查询