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

Tsql 如何计算两个日期之间的PaidAmount之和,tsql,Tsql,我有三张表,分别是账目、付款、报表。表帐户包含所有帐户,表付款包含对帐户的所有付款,表报表包含帐户的所有报表数据 帐目 账户ID |死亡日期| 1001 | 2014-03-10| 付款 账户ID | PaidAmount |付款日期 1001 | 80.27 | 2014-07-09 1001 | 80.27 | 2014-06-10 1001 | 80.27 | 2014-05-12 1001 | 80.27 | 2014-04-13 1001 | 80.27 | 2014-03-15 10

我有三张表,分别是账目、付款、报表。表帐户包含所有帐户,表付款包含对帐户的所有付款,表报表包含帐户的所有报表数据

帐目

账户ID |死亡日期|

1001 | 2014-03-10|

付款

账户ID | PaidAmount |付款日期

1001 | 80.27 | 2014-07-09

1001 | 80.27 | 2014-06-10

1001 | 80.27 | 2014-05-12

1001 | 80.27 | 2014-04-13

1001 | 80.27 | 2014-03-15

1001 | 80.27 | 2014-02-14

声明

账户ID |余额|报表日期

1001 | 0.00 | 2014-03-28

1001 | 1909.31 | 2014-02-25

我需要知道付款表中PaidAmount表付款的金额,该金额介于2014-03-28和2014-02-25的报表日期表之间。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

你可以用几种方法

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'
同样,如果您不想做案例陈述,可以在where clase中添加

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