Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frndz,

I tried some cte in this example and the result is totally wrong. plz see my example

SQL
declare @table table
(
entrydate datetime,
openingbal decimal(15,3),
credit decimal(15,3),
debit decimal(15,3),
closingbal decimal(15,3)
)
insert into @table
select '2014-10-02 23:05:43.203',	94000.000,	0.000,	900.000	,93100.000
union
select '2014-10-03 23:05:43.203',	0.000	,1000.000	,0.000,	0.000
union
select '2014-10-03 23:06:43.203',	0.000,	0.000,	500.000	,0.000
union
select '2014-10-14 23:52:27.327',	4000.000	,0.000	,1500.000	,2500.000
--select * from @table
 ;
with cte as(
select *,  ROW_NUMBER()over (order by entrydate)rid from @table
)
, cte1 as(
select entrydate ,cast (openingbal as decimal(15,3)) openingbal,credit,debit, cast (closingbal as decimal(15,3))closingbal ,
 ROW_NUMBER()over (order by entrydate)rid
from  cte
union all
select  b.entrydate ,cast (a.closingbal as decimal(15,3))  openingbal ,b.credit,b.debit,cast ((a.closingbal+b.credit) -b.debit as decimal(15,3)) closingbal
 ,ROW_NUMBER()over (order by a.entrydate) rid
from cte a
join cte1 b on  b.rid=a.rid+1
)
select  entrydate ,isnull(openingbal,0)openingbal,isnull(credit,0) CreditAmount
,isnull(debit,0) DebitAmount,isnull(closingbal,openingbal)closingbal ,rid from cte1 order by entrydate,rid 
--------------------------------------------------------------------------
but i want to generate the opening bal based on prev row closingbal like this restult
entryate	opnbal	  cr	   dr	closingbal
02-10-14 23:05  94000      0      900   93100
03-10-14 23:05  93100   1000        0   94100
03-10-14 23:06  94100      0      500   93600
14-10-14 23:52  97600      0     1500   96100 

plz help me.
Posted
Updated 16-Jul-20 5:36am
v4

You are on the right track, but cte1 is wrong. You need to select the first row of cte as your anchor. Then join next row while calculating the right opening and closing balances.

SQL
...
, cte1 as(
  select entrydate, openingbal, credit, debit, cast(openingbal + credit - debit as decimal(15,3)) as closingbal, rid
  from cte where rid = 1
  union all
  select curr.entrydate, cast(curr.openingbal + prev.closingbal as decimal(15,3)), curr.credit, curr.debit, 
    cast(curr.openingbal + prev.closingbal + curr.credit - curr.debit as decimal(15,3)) as closingbal, curr.rid
  from cte curr inner join cte1 prev on curr.rid = prev.rid + 1
)
select *
from cte1
order by entrydate
 
Share this answer
 
Comments
Member 9313444 9-Nov-14 0:27am    
thank you very much
with cte as(
select *,  ROW_NUMBER()over (order by entrydate)rid from #table
), cte1 as(
  select entrydate, openingbal, credit, debit, cast(openingbal + credit - debit as decimal(15,3)) as closingbal, rid
  from cte where rid = 1
  union all
  select curr.entrydate, cast(prev.closingbal as decimal(15,3)), curr.credit, curr.debit, 
    cast(prev.closingbal + curr.credit - curr.debit as decimal(15,3)) as closingbal, curr.rid
  from cte curr inner join cte1 prev on curr.rid = prev.rid + 1
)
select *
from cte1
order by entrydate
 
Share this answer
 
v2
Comments
Member 14172816 6-Mar-19 4:56am    
Last Row Opening balance Amount Not Consider In this Query
Solution 4
SELECT * FROM @table AS mainDtl OUTER APPLY (SELECT SUM(subDtl.openingbal)+SUM(subDtl.credit)-SUM(subDtl.debit) closing FROM @table AS subDtl WHERE subDtl.entrydate<=mainDtl.entrydate)Data
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900