判断收入或支出,在同一列字段里相加或相减(200分)

J

jmlwz

Unregistered / Unconfirmed
GUEST, unregistred user!
收支类型 金额
收入 2
收入 2
支出 3
支出 3
合计: 2 (Footer) <<求出结果
 
就是使用 DBGridEh 与 ClientDataSet
求方法如何做
 
/*
收支类型 金额
收入 2
收入 2
支出 3
支出 3
*/
create table tb ([收支类型] varchar(10),[金额] money )
insert into tb values('收入',2)
insert into tb values('收入',2)
insert into tb values('支出',3)
insert into tb values('支出',3)

select sum(case [收支类型] when '收入' then
[金额]*(-1) when '支出' then
[金额] else
0 end ) from tb
drop table tb
 
/*
收支类型 金额
收入 2
收入 2
支出 3
支出 3
*/
create table tb ([收支类型] varchar(10),[金额] money )
insert into tb values('收入',2)
insert into tb values('收入',2)
insert into tb values('支出',3)
insert into tb values('支出',3)

--select sum(case [收支类型] when '收入' then
[金额]*(-1) when '支出' then
[金额] else
0 end ) from tb
--写法2
select (select sum([金额]) from tb where [收支类型]='支出')-(select sum([金额]) from tb where [收支类型]='收入')
drop table tb
 
多谢,hfghfghfg ,你的方法能行,可能我说得不清楚,能否在Delphi环境里做到,
 
那你就
while not eof
自己计算
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部