create table t_department
(
dept_id varchar(50),
dept_name varchar(50)
)
insert into t_department select '01','总部门'
insert into t_department select '011','部门1'
insert into t_department select '012','部门2'
create table t_rb_total --项目金额统计表
(
xm varchar(50),--项目名称
dr numeric(13,2),--当日发生金额
rq datetime,--日期
ssbm varchar(50)--所属部门
)
insert into t_rb_total select '增加信誉值金额合计',1,'2008-3-15','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',2,'2008-3-15','部门1' insert into t_rb_total select '增加信誉值金额合计',10,'2008-3-16','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',20,'2008-3-16','部门1' insert into t_rb_total select '增加信誉值金额合计',30,'2008-3-17','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',40,'2008-3-17','部门1'
insert into t_rb_total select '增加信誉值金额合计',1,'2008-3-15','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',2,'2008-3-15','部门2' insert into t_rb_total select '增加信誉值金额合计',10,'2008-3-16','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',20,'2008-3-16','部门2' insert into t_rb_total select '增加信誉值金额合计',30,'2008-3-17','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',40,'2008-3-17','部门2'select * from t_department
select * from t_rb_total
-----------------------------------------------------------------------------
/*
要产生的报表格式为
往日营业厅余额合计 累计1 当日营业厅余额合计 累计2 日期 所属部门
0 0 -2 -2 2008-3-15 总部门
-2 -2 -22 -24 2008-3-16 总部门
-22 -24 -42 -66 2008-3-17 总部门
*/
/*
业务流程说明:
往日营业厅余额合计=前一日的'当日营业厅余额合计',如果'前一日的当日营业厅余额合计'
为0,则'往日营业厅余额合计'也为0,例如:如果从2008-3-15这天
开始计费,那么它的往日合作厅余额合计,也就是2008-3-14这天的余额
合计为0
当日合作厅余额合计=往日合作厅余额合计 + 增加信誉值金额合计 - (已上)合作厅及代理商应收 累计1=往日合作厅余额合计的累加数。例如:
从2008-3-15到2008-3-17这3天内,每一天的累加数为
2008-3-15 = 0
2008-3-16 = 0 + (-1)=-1
2008-3-17 = 0 + (-1) + (-11)=-12
累计2=当日合作厅余额合计的累加数。例如:
从2008-3-15到2008-3-17这3天内,每一天的累加数为
2008-3-15 = -1
2008-3-16 = -1 + (-11) =-12
2008-3-17 = -1 + (-11) + (-21) = -33 因为有很多部门,所以要能够按照日期和部门进行查询
-------------------------------------------------------------------------------------
其中:其中单个部门的语句已经有了,例如部门1
if object_id('tempdb.dbo.#') is not null drop table #
create table # --项目金额统计表
(
xm varchar(50),--项目名称
dr numeric(13,2),--当日发生金额
rq datetime,--日期
ssbm varchar(50)--所属部门
)
insert into # select '增加信誉值金额合计',1,'2008-3-15','部门1'
insert into # select '(已上)合作厅及代理商应收',2,'2008-3-15','部门1'
insert into # select '增加信誉值金额合计',10,'2008-3-16','部门1'
insert into # select '(已上)合作厅及代理商应收',20,'2008-3-16','部门1'
insert into # select '增加信誉值金额合计',30,'2008-3-17','部门1'
insert into # select '(已上)合作厅及代理商应收',40,'2008-3-17','部门1'--> 代码简洁起见,用个临时表
if object_id('tempdb.dbo.#Temp') is not null drop table #Temp
select
a=(select isnull(sum(a.dr-b.dr),0) from # a join # b on a.ssbm=b.ssbm and a.rq=b.rq where a.rq<t.rq and a.xm='增加信誉值金额合计' and b.xm='(已上)合作厅及代理商应收'),
b=(select sum(a.dr-b.dr) from # a join # b on a.ssbm=b.ssbm and a.rq=b.rq where a.rq<=t.rq and a.xm='增加信誉值金额合计' and b.xm='(已上)合作厅及代理商应收'),
rq, ssbm
into #Temp
from # t where xm='增加信誉值金额合计'--> 二次累计
select
往日营业厅余额合计=a,
累计1=(select sum(a) from #Temp where ssbm=t.ssbm and rq<=t.rq),
当日营业厅余额合计=b,
累计2=(select sum(b) from #Temp where ssbm=t.ssbm and rq<=t.rq),
日期=rq,
所属部门=ssbm
from #Temp t where t.ssbm='部门1'/*
报表为
往日营业厅余额合计 累计1 当日营业厅余额合计 累计2 日期 所属部门
0 0 -1 -1 2008-3-15 部门1
-1 -1 -11 -12 2008-3-16 部门1
-11 -12 -21 -33 2008-3-17 部门1
*/
----------------------------------------------------------------------------
--现在要算出总部门的数据,就是要得出总部门下属的所有分部门(部门1,部门2)的数据总和
--改怎样改写已上的SQL语句
*/
drop table t_rb_total
drop table t_department
往日营业厅余额合计=a,
累计1=(select sum(a) from #Temp where ssbm=t.ssbm and rq <=t.rq),
当日营业厅余额合计=b,
累计2=(select sum(b) from #Temp where ssbm=t.ssbm and rq <=t.rq),
日期=rq,
所属部门=ssbm
from #Temp t where t.ssbm='部门1'
--------------------------------------------------------------------
insert #temp
select sum(往日营业厅余额合计),sum(累计1),sum(当日营业厅余额合计),sum(累计2),日期,'合计'
from #temp group by 所属部门
select
往日营业厅余额合计=a,
累计1=(select sum(a) from #Temp where ssbm=t.ssbm and rq <=t.rq),
当日营业厅余额合计=b,
累计2=(select sum(b) from #Temp where ssbm=t.ssbm and rq <=t.rq),
日期=rq,
所属部门=ssbm
into #hz
from #Temp tinsert #hz
select sum(往日营业厅余额合计),sum(累计1),sum(当日营业厅余额合计),sum(累计2),日期,'合计'
from #hz group by 所属部门
(
dept_id varchar(50),
dept_name varchar(50)
)
insert into t_department select '01','总部门'
insert into t_department select '011','部门1'
insert into t_department select '012','部门2'
create table t_rb_total --项目金额统计表
(
xm varchar(50),--项目名称
dr numeric(13,2),--当日发生金额
rq datetime,--日期
ssbm varchar(50)--所属部门
)
insert into t_rb_total select '增加信誉值金额合计',1,'2008-3-15','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',2,'2008-3-15','部门1' insert into t_rb_total select '增加信誉值金额合计',10,'2008-3-16','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',20,'2008-3-16','部门1' insert into t_rb_total select '增加信誉值金额合计',30,'2008-3-17','部门1'
insert into t_rb_total select '(已上)合作厅及代理商应收',40,'2008-3-17','部门1'
insert into t_rb_total select '增加信誉值金额合计',1,'2008-3-15','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',2,'2008-3-15','部门2' insert into t_rb_total select '增加信誉值金额合计',10,'2008-3-16','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',20,'2008-3-16','部门2' insert into t_rb_total select '增加信誉值金额合计',30,'2008-3-17','部门2'
insert into t_rb_total select '(已上)合作厅及代理商应收',40,'2008-3-17','部门2' select * from t_department
select * from t_rb_total
-----------------------------------------------------------------------------
/*
要产生的报表格式为
往日营业厅余额合计 累计1 当日营业厅余额合计 累计2 日期 所属部门
0 0 -2 -2 2008-3-15 总部门
-2 -2 -22 -24 2008-3-16 总部门
-22 -24 -42 -66 2008-3-17 总部门
*/ select * into #mytable
from
(
select
t1.dr as '(已上)合作厅及代理商应收',
t2.dr as '增加信誉值金额合计',
t1.rq as date_,
t1.ssbm as department
from t_rb_total t1
join t_rb_total t2 on t1.rq=t2.rq and t1.ssbm=t2.ssbm and t1.xm<>t2.xm
and t1.xm<>'增加信誉值金额合计'
) aselect
isnull((select sum(t2.[增加信誉值金额合计]-t2.[(已上)合作厅及代理商应收] )
from #mytable t2
where t1.department=t2.department
and t1.date_>t2.date_),0) 往日营业厅余额合计 ,
isnull((select sum(t2.[增加信誉值金额合计]-t2.[(已上)合作厅及代理商应收] )
from #mytable t2
where t1.department=t2.department
and t1.date_>=t2.date_),0) 当日营业厅余额合计 ,
department,date_
into #mytable2
from #mytable t1 select
往日营业厅余额合计,
isnull((select sum(t2.往日营业厅余额合计 )
from #mytable2 t2
where t1.department=t2.department
and t1.date_>=t2.date_),0) 累计1 ,
当日营业厅余额合计,
isnull((select sum(t2.当日营业厅余额合计 )
from #mytable2 t2
where t1.department=t2.department
and t1.date_>=t2.date_),0) 累计2 ,
department,date_
from #mytable2 t1 drop table #mytable2
drop table #mytable