/*
有一个表fee,是记账用的,结构和数据如下:
CREATE TABLE [Fee] (
[FeeID] [int] IDENTITY (1, 1) NOT NULL ,
[FeeDate] [datetime] NOT NULL ,
[FeeType] [int] NOT NULL ,--操作类型,1为收入,2为支出,3为存款,4为取款
[FeeInBank] [int] NULL ,--流入银行ID, 0为现金
[FeeIn] [decimal](18, 2) NULL DEFAULT (0),--流入金额
[FeeOutBank] [int] NULL ,--流出银行ID, 0为现金
[FeeOut] [decimal](18, 2) NULL DEFAULT (0) ,--流出金额
[FeeSaveBank] [int] NULL ,--存款银行,不为现金
[FeeSave] [decimal](18, 2) NULL DEFAULT (0),--存款金额
[FeeTakeBank] [int] NULL ,--取款银行,不为现金
[FeeTake] [decimal](18, 2) NULL DEFAULT (0)--取款金额
CONSTRAINT [PK_Fee] PRIMARY KEY CLUSTERED
(
[FeeID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
select * from fee
Insert Into Fee (FeeDate,FeeType,FeeInBank,FeeIn) values ('2009-10-1',1,0,2000) --现金收入
Insert Into Fee (FeeDate,FeeType,FeeInBank,FeeIn) values ('2009-10-2',1,1,1000) --银行一收入
Insert Into Fee (FeeDate,FeeType,FeeOutBank,FeeOut) values ('2009-10-3',2,0,200) --现金支出
Insert Into Fee (FeeDate,FeeType,FeeOutBank,FeeOut) values ('2009-10-4',2,1,20) --银行一支出
Insert Into Fee (FeeDate,FeeType,FeeSaveBank,FeeSave) values ('2009-10-6',3,2,100) --现金存入银行二
Insert Into Fee (FeeDate,FeeType,FeeTakeBank,FeeTake) values ('2009-10-7',4,1,500) --银行一取出现金
我现在要查指定日期-10-30的现金和银行余额
银行ID肯定是大于的
如代表建行
2代表工行
除了随你定义
现金存入银行二
代表现金减少,银行二增加
取款也是类似
只是相反
现金增加,银行减少
格式比如:
账户 排序 账户余额
现金 0 2000.00
建行 1 -300.00
建行 2 500.00
农业银行 2 80.00
*/
解决方法:
CREATE TABLE [Fee] (
[FeeID] [int] IDENTITY (1, 1) NOT NULL ,
[FeeDate] [datetime] NOT NULL ,
[FeeType] [int] NOT NULL ,--操作类型,1为收入,2为支出,3为存款,4为取款
[FeeInBank] [int] NULL ,--流入银行ID, 0为现金
[FeeIn] [decimal](18, 2) NULL DEFAULT (0),--流入金额
[FeeOutBank] [int] NULL ,--流出银行ID, 0为现金
[FeeOut] [decimal](18, 2) NULL DEFAULT (0) ,--流出金额
[FeeSaveBank] [int] NULL ,--存款银行,不为现金
[FeeSave] [decimal](18, 2) NULL DEFAULT (0),--存款金额
[FeeTakeBank] [int] NULL ,--取款银行,不为现金
[FeeTake] [decimal](18, 2) NULL DEFAULT (0)--取款金额
CONSTRAINT [PK_Fee] PRIMARY KEY CLUSTERED
(
[FeeID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
Insert Into Fee (FeeDate,FeeType,FeeInBank,FeeIn) values ('2009-10-1',1,0,2000) --现金收入
Insert Into Fee (FeeDate,FeeType,FeeInBank,FeeIn) values ('2009-10-2',1,1,1000) --银行一收入
Insert Into Fee (FeeDate,FeeType,FeeOutBank,FeeOut) values ('2009-10-3',2,0,200) --现金支出
Insert Into Fee (FeeDate,FeeType,FeeOutBank,FeeOut) values ('2009-10-4',2,1,20) --银行一支出
Insert Into Fee (FeeDate,FeeType,FeeSaveBank,FeeSave) values ('2009-10-6',3,2,100) --现金存入银行二
Insert Into Fee (FeeDate,FeeType,FeeTakeBank,FeeTake) values ('2009-10-7',4,1,500) --银行一取出现金
---匹配表----------
create table daihao(id int , name varchar(10))
insert daihao select
1,'建行' union all select
2,'建行'
go
---封装成存储过程--------
create proc k
as
begin
create table #ps (账户 varchar(10), 账户余额 int)
declare @s varchar(8000)
set @s=''
select @s=@s+' union all
select 账户='''+name+''',
账户余额=SUM(case when FeeInBank='+RTRIM(ID)+ ' then FeeIn when FeeType=3 and FeeSaveBank='+RTRIM(ID)+' then FeeSave else 0 end)
-SUM(case when FeeOutBank='+RTRIM(ID) +' then FeeOut when FeeType=4 and FeeTakeBank='+RTRIM(ID)+' then FeeTake else 0 end)'+
'
from Fee , daihao
where (fee.[FeeInBank]=daihao.id or fee.FeeOutBank=daihao.id or fee.FeeSaveBank=daihao.id or fee.FeeTakeBank=daihao.id) and
DATEDIFF(DAY,FeeDate,''2009-10-30'')>=0
'
from Fee , daihao
where (fee.[FeeInBank]=daihao.id or fee.FeeOutBank=daihao.id or fee.FeeSaveBank=daihao.id or fee.FeeTakeBank=daihao.id) and
DATEDIFF(DAY,FeeDate,'2009-10-30')>=0
set @S='select
账户=''现金'',
账户余额=SUM(case when feetype=1 and FeeInBank=0 then FeeIn when feetype=4 then FeeTake else 0 end)
-SUM(case when feetype=2 and FeeOutBank=0 then FeeOut when feetype=3 then FeeSave else 0 end)
from Fee where DATEDIFF(DAY,FeeDate,''2009-10-30'')>=0 '+@s
insert #ps(账户,账户余额)
exec(@s)
select 账户,
排序=case when a.name IS null then 0 else id end ,
账户余额
from (select distinct * from #ps) k left join daihao a on k.账户=a.name
order by 排序
end
----执行存储过程--------
exec k
/*
--这是你增加前的户头---
/*
账户 排序 账户余额
---------- ----------- -----------
现金 0 2200
建行 1 480
建行 2 100
*/*/
-----------------下面我演示下你增加用户的过程--------------
---------1.往匹配表daihao里添加你的用户---------
insert daihao values (3,'t-mac银行')
------2.往你的FEE表添加资金流动------------------
Insert Into Fee (FeeDate,FeeType,FeeInBank,FeeIn) values ('2009-10-5',1,0,2000) --现金收入
Insert Into Fee (FeeDate,FeeType,FeeSaveBank,FeeSave) values ('2009-10-6',3,3,1000) --现金存入银行1000
Insert Into Fee (FeeDate,FeeType,FeeTakeBank,FeeTake) values ('2009-10-7',4,3,500) --银行取出现金
----------3.调用查看-----------------
exec k
/*
账户 排序 账户余额、
---------- ----------- -----------
现金 0 3700 ---注意这里本来你的现金是后来收入存入取出就是+2000-1000+500=3700
建行 1 480
建行 2 100
t-mac银行 3 500*/