表:
编号 日期 金额
1 2009-04-07 100
2 2009-04-07 -200
3 2009-04-07 50
4 2009-04-06 100
5 2009-04-04 100
6 2009-04-06 -10 实现结果:
日期 借方金额 贷方金额 方向 合计
2009-04-04 100 0 借方 100
2009-04-06 100 10 借方 190
2009-04-07 150 200 贷方 140
=======================================================
借-贷>0方向为借
借-贷<0方向为贷我不知道“方向”怎么用存储过程做,有知道请告诉我下,谢谢。
以下是我写的:
create procedure proc_cal
(
@startdate datetime,
@enddate datetime
)
as
begin transactiondeclare @A table (编号 int,日期 datetime,金额 int)
insert @A values(1,'2009-04-07',100)
insert @A values(2,'2009-04-07',-200)
insert @A values(3,'2009-04-07',50)
insert @A values(4,'2009-04-06',100)
insert @A values(5,'2009-04-04',100)
insert @A values(6,'2009-04-06',-10)
select 日期,借方金额= sum(case when 金额>0 then 金额 else 0 end),
贷方金额=-sum(case when 金额<0 then 金额 else 0 end),
余额=(select sum(金额) from @A where 日期<=a.日期 )
from @A a
group by 日期
commit transaction
GOexec proc_cal '2009-04-04','2009-04-07'=======================================================
日期 借方金额 贷方金额 余额
------------------------------------------------------ ----------- ----------- -----------
2009-04-04 00:00:00.000 100 0 100
2009-04-06 00:00:00.000 100 10 190
2009-04-07 00:00:00.000 150 200 140(所影响的行数为 3 行)
编号 日期 金额
1 2009-04-07 100
2 2009-04-07 -200
3 2009-04-07 50
4 2009-04-06 100
5 2009-04-04 100
6 2009-04-06 -10 实现结果:
日期 借方金额 贷方金额 方向 合计
2009-04-04 100 0 借方 100
2009-04-06 100 10 借方 190
2009-04-07 150 200 贷方 140
=======================================================
借-贷>0方向为借
借-贷<0方向为贷我不知道“方向”怎么用存储过程做,有知道请告诉我下,谢谢。
以下是我写的:
create procedure proc_cal
(
@startdate datetime,
@enddate datetime
)
as
begin transactiondeclare @A table (编号 int,日期 datetime,金额 int)
insert @A values(1,'2009-04-07',100)
insert @A values(2,'2009-04-07',-200)
insert @A values(3,'2009-04-07',50)
insert @A values(4,'2009-04-06',100)
insert @A values(5,'2009-04-04',100)
insert @A values(6,'2009-04-06',-10)
select 日期,借方金额= sum(case when 金额>0 then 金额 else 0 end),
贷方金额=-sum(case when 金额<0 then 金额 else 0 end),
余额=(select sum(金额) from @A where 日期<=a.日期 )
from @A a
group by 日期
commit transaction
GOexec proc_cal '2009-04-04','2009-04-07'=======================================================
日期 借方金额 贷方金额 余额
------------------------------------------------------ ----------- ----------- -----------
2009-04-04 00:00:00.000 100 0 100
2009-04-06 00:00:00.000 100 10 190
2009-04-07 00:00:00.000 150 200 140(所影响的行数为 3 行)
insert @A values(1,'2009-04-07',100)
insert @A values(2,'2009-04-07',-200)
insert @A values(3,'2009-04-07',50)
insert @A values(4,'2009-04-06',100)
insert @A values(5,'2009-04-04',100)
insert @A values(6,'2009-04-06',-10)
select 日期,借方金额= sum(case when 金额>0 then 金额 else 0 end),
贷方金额=-sum(case when 金额<0 then 金额 else 0 end),
方向=CASE WHEN sum(case when 金额>0 then 金额 else 0 end)-(-sum(case when 金额<0 then 金额 else 0 end))>0 THEN '借' ELSE '贷' END,
余额=(select sum(金额) from @A where 日期<=a.日期 )
from @A a
group by 日期
(
@startdate datetime,
@enddate datetime
)
as
begin transactiondeclare @A table (编号 int,日期 datetime,金额 int)
insert @A values(1,'2009-04-07',100)
insert @A values(2,'2009-04-07',-200)
insert @A values(3,'2009-04-07',50)
insert @A values(4,'2009-04-06',100)
insert @A values(5,'2009-04-04',100)
insert @A values(6,'2009-04-06',-10)
select *,方向=case when 借方金额>贷方金额 then '借方' else '贷方' end from(
select 日期,借方金额= sum(case when 金额>0 then 金额 else 0 end),
贷方金额=-sum(case when 金额<0 then 金额 else 0 end),
余额=(select sum(金额) from @A where 日期<=a.日期 )
from @A a
group by 日期)a commit transaction
GOexec proc_cal '2009-04-04','2009-04-07'drop proc proc_cal/*
日期 借方金额 贷方金额 余额 方向
----------------------- ----------- ----------- ----------- ----
2009-04-04 00:00:00.000 100 0 100 借方
2009-04-06 00:00:00.000 100 10 190 借方
2009-04-07 00:00:00.000 150 200 140 贷方
*/
from
(
select 日期,借方金额= sum(case when 金额>0 then 金额 else 0 end),
贷方金额=-sum(case when 金额<0 then 金额 else 0 end),
余额=(select sum(金额) from @A where 日期<=a.日期 )
from @A a
group by 日期
)