情景:我正在使用用戶帳戶,其中用戶向帳戶(貸方)添加金額並且他們從其帳戶(借項)中提取了他們的願望金額,但一切都正確用戶信用卡或借記卡在相同的日期,它給了我錯誤的結果(餘額)。這裏refno是用戶的參考。這裏是我的查詢借方,貸方在同一日期沒有顯示正確的結果
declare @startdate date='2013-01-02',
@enddate date='2013-01-12';
With summary(id,refno,transdate,cr,dr,balance)
as
(
select id,
RefNo,
Cast(TransDate as Varchar),
cr,
dr,
(cr-dr)+(Select ISNULL(Sum(l.Cr-l.Dr) ,0)
From Ledger l
Where l.TransDate<Ledger.TransDate and refno=001) as balance
from Ledger
),
openingbalance(id,refno,transdate,cr,dr,balance)
As (
select top 1 '' id,'OPENING BAL','','','',balance
from summary
where transdate<@startdate
order by transdate desc
)
select *
from openingbalance
union
Select *
From summary
where transdate between @startdate and @enddate and refno=001 order by transdate
這是因爲在後內部子查詢相同date.add一個條件和引用號= 001 和排除博士屬於ID 6 – KumarHarsh