如何计算两个日期之间的PaidAmount总和

时间:2014-09-22 23:43:09

标签: tsql

我有3个表,即帐户,付款,报表。表帐户包含所有帐户,表格付款包含对帐户的所有付款,表格表格包含帐户的所有帐单数据。

帐户


AccountID | DateOfDeath |


1001 | 2014-03-10 |

付款


AccountID |付费安装| PaymentDate


1001 | 80.27 | 2014年7月9日


1001 | 80.27 | 2014年6月10日


1001 | 80.27 | 2014年5月12日


1001 | 80.27 | 2014年4月13日


1001 | 80.27 | 2014-03-15


1001 | 80.27 | 2014年2月14日



AccountID |平衡| STATEMENTDATE


1001 | 0.00 | 2014年3月28日


1001 | 1909.31 | 2014年2月25日

我需要知道Payments表中PaidAmount(表付款)的总和,该表位于2014-03-28和2014-02-25的StatementDate(表格声明)之间。 PaidAmount的总和应该是80.27,但我得到321.08。任何人都可以告诉我我做错了什么,或者我怎样才能更好地编写查询?

这是我到目前为止所拥有的

create table #temp1
(
    AccountID Numeric(9, 0)
    , DateOfDeath date
    , StatementDate date
    , Balance numeric(17,2)
)


insert into #temp1
(
    AccountID, DateOfDeath, StatementDate, Balance
)

select a.AccountID
  ,DateofDeath
  ,StatementDate
  ,Balance
from Accounts a 
inner join Statements b on a.accountID = b.accountID
where StatementDate in (select top 1 statementdate
                         from Statements
                    where AccountID = a.AccountID
                    and StatementDate >= DateOfDeath
                    order by StatementDate)
Order By a.AccountID, StatementDate

create table #temp2
(
    AccountId Numeric(9,0)
   , PaidAmount Numeric(10, 2)
   , PaymentDate date
)

select a.accountid, sum(a.Paidamount), max(a.PaymentDate)
from tblCreditDefenseInceptionToDateBenefit a
inner join #temp1 b on a.accountid = b.accountid
where a.paymentdate <= (select top 1 StatementDate from Statements
                        where AccountID = a.accountid
                   and statementdate >= b.dateofdeath
                   order by StatementDate desc)
and a.paymentdate > (select top 1 StatementDate from Statements 
                   where AccountID = a.accountid
                   and statementdate < b.dateofdeath
                   order by StatementDate desc)
group by a.accountid
order by a.accountid desc

select * from #temp2

drop table #temp1
drop table #temp2

1 个答案:

答案 0 :(得分:1)

你可以通过几种方式解决这个问题

Create table #accounts
(AccountID int, Date_Death date)

insert into #accounts
(accountID, Date_death)
values 
('1001', '03/10/2014')

Create Table #payments
(AccountID int,  paidamt decimal(6,2), paymentdt date)
insert into #payments
(AccountID ,  paidamt, paymentdt)
values
('1001',    '80.27','07/09/2014'),
('1001',    '80.27','06/10/2014'),
('1001',    '80.27','05/12/2014'),
('1001',    '80.27','04/13/2014'),
('1001',    '80.27','03/15/2014'),
('1001',    '80.27','02/14/2014')
;


with cte as (
select 
Accountid,
case when paymentdt between '02/25/2014'and '03/28/2014' then (paidamt) else null  end as paidamt


from
#payments
)

Select
accountid,
SUM(paidamt)

from cte

group by
AccountID

把它放在where子句而不是做一个case语句,真的取决于你的风格

select
accountid,
sum(paidamt)paidamt

from 
#payments

where paymentdate >= '02/25/2014'
and paymentdate <= '03/282014'

如果要将语句表日期用作参数

with cte as
(
select 
a.AccountID, 
case when a.paymentdt between b.min_dt and b.max_dt then a.paidamt else null end as 'pdamt' 

from 
#payments as a
inner join 
        (select accountid, MIN(statementdt)min_dt, MAX(statementdt)max_dt from #statement group by accountid) as b on b.accountid = a.AccountID 
)

select 
AccountID, 
SUM(pdamt) as 'Paid Amount' 

from
cte

group by 
AccountID

再次,如果你不想做案件的话,可以加在clase中