SQL查询 - 信用卡,借记卡,余额

时间:2015-01-05 12:01:24

标签: mysql sql

免责声明:我知道这已被多次询问,但我想要的只是另一种选择。

表格如下:

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);

预期结果很简单:

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

有效的查询是:

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

我想要的是,对于相同的结果,是否有任何查询没有'case'语句(如子查询或自联接)?

4 个答案:

答案 0 :(得分:2)

不确定。您可以使用带有where子句和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

或者,使用带有内联视图的join

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

说实话,我建议使用case ...

答案 1 :(得分:0)

使用char的ASCII代码并尝试从那里开始。对于' d'并且99为' c。未经测试的例子:

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

我不建议使用此方法,但它是实现您想要的方式。

答案 2 :(得分:0)

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;

这肯定会帮助你!!

答案 3 :(得分:0)

以下内容在Microsoft SQL Server上对我有用。它也具有结转余额

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