Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two tables A and B
In A

MobileNo Credit Date
9888569493 10000 30-Dec-16


In B

MobileNo Debit Date
9888569493 81 3-Apr-17
9888569493 261 3-Apr-17
9888569493 500 4-Apr-17
9888569493 1000 4-Apr-17

So I want my output like given below:

MobileNo Date Credit Debit Balance
9888569493 30-Dec-16 10000 0 10000
9888569493 3-Apr-17 81 9919
9888569493 3-Apr-17 261 9658
9888569493 4-Apr-17 500 9158
9888569493 4-Apr-17 1000 8158


So please help me about this complex query at urgent basis

Thanks in advance.

What I have tried:

I tried this one:

select d.* from(select Date,Credit,Debit, sum(Credit)-sum(Debit) as Balance from(

Select convert(varchar(10),date ,103) as Date, ISNULL(SUM(CAST(Runs as int)),0) as Credit ,
0 as Debit From UserMobile Where UserMobile .REtailer ='9888569493' And month ='October' And year =2016
Group By UserMobile.date

Union All

Select convert(varchar(10),date ,103) as Date, 0 as Credit ,ISNULL(SUM(CAST(Runs as int)),0) as debit
From DisptachOrder Where UserName =9888569493
Group By date)as c group by Date ,Credit,Debit) as d

And my Output is:
Date Credit Debit Balance
NULL 100000 0 100000
04/04/2017 0 81 -81
04/04/2017 0 99 -99
04/04/2017 0 117 -117
04/04/2017 0 252 -252
Posted
Updated 5-Oct-18 19:53pm

 
Share this answer
 
Comments
Member 12693375 5-Apr-17 4:54am    
Thanks for the reply.but i have two tables so how is possible to get the running total after credit-debit.
OriginalGriff 5-Apr-17 5:30am    
Use a JOIN...
Member 12693375 5-Apr-17 5:52am    
i used join but no exact result.can u make a query for this problem.plz help
OriginalGriff 5-Apr-17 5:52am    
What JOIN did you try?
Member 12693375 5-Apr-17 5:58am    
Inner Join but i hv two tables A and B
IN A i have 3 columns : mobileno,creditpoints,date
IN B i have 3 columns : mobileno,debitpoints,date

so i want the output like
Date Mobileno Credit Debit Balance
-- 9888xxx 10000 0 10000
-- 9888xxx 0 500 9500
-- 9888xxx 0 300 9200

And date is differnt.
use below query
SELECT  MobileNo,convert(varchar(10),Date,103) as Date,
        Credit, 
        Debit, 
        SUM(isnull(Credit,0) - isnull(Debit,0)) OVER (ORDER BY Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as Balance
FROM (Select Date,MobileNo,Credit,0 as Debit from A
Union all
Select Date,MobileNo,0 as Credit,Debit from B) as x
order by Date
 
Share this answer
 
Comments
Dave Kreskowiak 4-Oct-18 10:39am    
This was asked a year and a half ago. I seriously doubt the OP is still looking for an answer.
Salman622 9-Oct-18 7:09am    
I have posted the answer because may be it would be helpful for others
 Collect data From Table
-------------------------------------
  SELECT * FROM tb_Debit
  select * from tb_credit
  
----------------------------------------
work start from here
-------------------------------------  
   create table #t
   (
     MobileNo Varchar(15),
     Credit float,
     Debit float,
     TrnDate Datetime
   ) 
  
   insert into #t
   (
     MobileNo,Credit,Debit,TrnDate
   )
   Select MobileNo,credit,debit,trnDate
   from
   (
    select MobileNo,credit,0 as debit,trnDate from tb_credit
    union all
    select MobileNo,0 as credit,debit,trnDate from tb_Debit
   )y
   order by trnDate asc
   
   --select * from #t
   
   with #ledger as
   (
      select 
			MobileNo,
			TrnDate,
			Credit,
			Debit,
			ROW_NUMBER()over(ORDER by trndate)as row
      from #t
   )
     SELECT 
			L1.MobileNo,
			L1.TrnDate,
			(ISNULL(MAX(L1.Credit),0)+ ISNULL(SUM(L2.Credit),0)- ISNULL(SUM(L2.Debit),0))  AS Opening,
			L1.Credit As Credit,
			L1.Debit As Debit,
			(ISNULL( L1.Credit,0) + ISNULL(SUM(L2.Credit),0)- ISNULL(SUM(L2.Debit),0)- ISNULL((L1.Debit),0)) As closing
     FROM #ledger L1
     LEFT JOIN #ledger L2
     ON L1.row>L2.row
     GROUP BY L1.MobileNo,L1.TrnDate,L1.Credit,L1.Debit



: It is the One Of the Best Solution For your question
 
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