2014-02-26 59 views
0

情景:我正在使用用戶帳戶,其中用戶向帳戶(貸方)添加金額並且他們從其帳戶(借項)中提取了他們的願望金額,但一切都正確用戶信用卡或借記卡在相同的日期,它給了我錯誤的結果(餘額)。這裏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 

Here is Result of my Query

+2

這是因爲在後內部子查詢相同date.add一個條件和引用號= 001 和排除博士屬於ID 6 – KumarHarsh

回答

0

的問題是因爲當您查詢以前的平衡,你只是在尋找那個有transdate早於當前記錄的記錄,因此這意味着有任何記錄相同的日期將不包括在內。

這裏的解決方案是使用更獨特的順序值,在您的示例中,您可以使用ID值作爲順序標識符。但是,ID值並不總是確保順序的最佳選擇。我建議擴展您的transdate列以使用更精確的值幷包含交易時間。如果你能保證在給定的秒內不會有多次交易,那麼秒數可能足夠精確,但無論你決定你需要有信心,都不會有任何重複。


在試圖提供一個代碼變化的解決方案,將與您現有的數據工作,你可以嘗試以下方法,它使用id值,以確定是否記錄之前對當前記錄:

更改以下行:

Where l.TransDate<Ledger.TransDate and refno=001) as balance 

這樣:

Where l.ID<Ledger.ID and refno=001) as balance 
+0

我做我的查詢帳戶更改的提示transferdate日期和時間,但它也給了我錯誤的結果,U會請建議我一些建議 –

+0

@SaeedKhan:我需要看到你的新表結構,例如數據(如您當前的屏幕截圖)。爲什麼它不起作用?與以前一樣的問題? – musefan

0

通過@musefan提示後,我對查詢進行了更改,並且按照我的要求工作。這裏是基於對日期基地

declare @startdate date='2013-01-02', 
     @enddate date='2013-01-12'; 


With summary(id,refno,transdate,cr,dr,balance) 
as 
(
select id, 
     RefNo, 
    TransDate, 
    cr, 
    dr, 
     (cr-dr)+(Select ISNULL(Sum(l.Cr-l.Dr) ,0) 
    From Ledger l 
    Where Cast(l.TransDate as datetime)< Cast(Ledger.TransDate as datetime) 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 id,refno,Cast(TransDate as varchar) as datetime,cr,dr,balance 
from openingbalance 
union 

Select id,refno,Cast(TransDate as varchar)as datetime,cr,dr,balance 
From summary 
where transdate between @startdate and @enddate and refno=001 order by Cast(TransDate as varchar) 

和另一個查詢ID查詢

declare @startdate date='2013-01-02', 
     @enddate date='2013-01-12'; 


With summary(id,refno,transdate,cr,dr,balance) 
as 
(
select id, 
     RefNo, 
    TransDate, 
    cr, 
    dr, 
     (cr-dr)+(Select ISNULL(Sum(l.Cr-l.Dr) ,0) 
    From Ledger l 
    Where l.id < Ledger.id 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 id,refno,Cast(TransDate as varchar) as datetime,cr,dr,balance 
from openingbalance 
union 

Select id,refno,Cast(TransDate as varchar)as datetime,cr,dr,balance 
From summary 

where transdate between @startdate and @enddate and refno=001 order by id