ssk
ssk

Reputation: 13

SQL query - credit , debit , balance

DISCLAIMER : I Know this has been asked numerous times, but all I want is an alternative.

The table is as below :

create table 
Account
(Name varchar(20),
 TType varchar(5),
 Amount int);

insert into Account Values
('abc' ,'c', 500),
 ('abc', 'c', 700),
 ('abc', 'd', 100),
 ('abc', 'd', 200),

('ab' ,'c', 300),
 ('ab', 'c', 700),
 ('ab', 'd', 200),
 ('ab', 'd', 200);

Expected result is simple:

 Name     Balance
 ------  -----------
ab         600
abc        900

The query that worked is :

select Name, sum(case TType when 'c' then Amount
                    when 'd' then Amount * -1 end) as balance 
from Account a1 
group by Name.

All I want is, is there any query sans the 'case' statement (like subquery or self join ) for the same result?

Upvotes: 1

Views: 3796

Answers (4)

Firas Shrourou
Firas Shrourou

Reputation: 715

The following worked for me on Microsoft SQL server. It has the Brought Forward balance as well

WITH tempDebitCredit AS (
Select 0 As Details_ID, null As Creation_Date, null As Reference_ID, 'Brought 
Forward' As Transaction_Kind, null As Amount_Debit, null As Amount_Credit, 
isNull(Sum(Amount_Debit - Amount_Credit), 0) 'diff'
From _YourTable_Name
where Account_ID = @Account_ID
And Creation_Date < @Query_Start_Date
Union All
SELECT a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, 
a.Amount_Debit, a.Amount_Credit, a.Amount_Debit - a.Amount_Credit 'diff'
FROM _YourTable_Name a
where Account_ID = @Account_ID
And Creation_Date >= @Query_Start_Date And Creation_Date <= @Query_End_Date
)

SELECT a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, 
a.Amount_Debit, a.Amount_Credit, SUM(b.diff) 'Balance'
FROM   tempDebitCredit a, tempDebitCredit b
WHERE b.Details_ID <= a.Details_ID
GROUP BY a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, 
a.Amount_Debit, a.Amount_Credit
Order By a.Details_ID Desc

Upvotes: 0

shrikant_phale
shrikant_phale

Reputation: 1

select t.Name, sum(t.cr) - sum(t.dr) as balance from (select Name, case TType when 'c' then sum(Amount) else 0 end as cr, case TType when 'd' then sum(Amount) else 0 end as dr from Account group by Name, TType) t group by t.Name;

This will surely help you!!

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157136

Sure. You can use a second query with a where clause and a union all:

select name
,      sum(Amount) balance
from   Account a1
where  TType when 'c'
group
by     Name
union
all
select name
,      sum(Amount * -1) balance
from   Account a1
where  TType when 'd'
group
by     Name

Or this, using a join with an inline view:

select name
,      sum(Amount * o.mult) balance
from   Account a1
join   ( select 'c' cd
         ,      1 mult
         from   dual
         union all
         select 'd'
         ,      -1
         from   dual
       ) o
on     o.cd = a1.TType
group
by     Name

To be honest, I would suggest to use case...

Upvotes: 2

Adrian Nasui
Adrian Nasui

Reputation: 1095

Use the ASCII code of the char and try to go from there. It is 100 for 'd' and 99 for 'c'. Untested example:

select Name, sum((ASCII(TType) - 100) * Amount * (-1))  + sum((ASCII(TType) - 99) * Amount * (-1)))) as balance from Account a1 group by Name.

I would not recommend using this method but it is a way of achieving what you want.

Upvotes: 0

Related Questions