CREATE procedure R_GzReport_financeTotal
@YearNum varchar(20)
as
set @YearNum =ltrim(Rtrim(@YearNum))
begin
select @YearNum as YearNum
into table1
select * from table1
for xml auto
create table #temp(
DeptID int,
DeptName varchar(50),
DeptSequence int,
[month] int,
month1 int,
month2 int,
month3 int,
month4 int,
month5 int,
month6 int,
month7 int,
month8 int,
month9 int,
month10 int,
month11 int,
month12 int
)
insert into #temp
select distinct(pm.DeptID),
pm.DeptName,
pm.DeptSequence,
month(gr.GatherDate) as [month],
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=1 ),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=2),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=3),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=4),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=5),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=6),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=7),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=8),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=9),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=10),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=11),
(select isnull(sum(convert(int,gr.GatherMoney)),'') where month(gr.GatherDate)=12)
from GatheringRecord gr,Person_Main pm
where year(gr.GatherDate)=@YearNum
and ConfirmDate is not NULL
and gr.perID=pm.id
group by pm.DeptSequence,pm.DeptID,pm.DeptName,month(gr.GatherDate)
order by pm.DeptSequence,month(gr.GatherDate)
select
DeptName,
avg(DeptSequence)as [sequence],
isnull(avg(month1),0) as getmoney1,
isnull(avg(month2),0) as getmoney2,
isnull(avg(month3),0) as getmoney3,
isnull(avg(month4),0) as getmoney4,
isnull(avg(month5),0) as getmoney5,
isnull(avg(month6),0) as getmoney6,
isnull(avg(month7),0) as getmoney7,
isnull(avg(month8),0) as getmoney8,
isnull(avg(month9),0) as getmoney9,
isnull(avg(month10),0) as getmoney10,
isnull(avg(month11),0) as getmoney11,
isnull(avg(month12),0) as getmoney12
from #temp
group by DeptName
order by avg(DeptSequence) for xml raw
drop table #temp,table1
end
GO
所有获得收入的部门的年总表(分部门和月份排列)
优化后的~~~
CREATE procedure R_GzReport_financeTotal
@YearNum varchar(20)
as
set @YearNum =ltrim(Rtrim(@YearNum))
begin
select @YearNum as YearNum
into table1
select * from table1
for xml auto
--创建临时表存放数据
create table #temp(
DeptSequence int,
DeptID int,
DeptName varchar(50),
gmm int,
summon int)
insert into #temp
select pm.deptsequence DeptSequence,
pm.deptid DeptID,
pm.deptname DeptName,
month(gr.GatherDate) gmm,
isnull(sum(convert(int,gr.GatherMoney)),'') summon
from person_main pm,gatheringrecord gr
where year(gr.GatherDate)=@YearNum
and ConfirmDate is not NULL
and gr.perID=pm.id
group by pm.DeptSequence,pm.DeptID,pm.DeptName,month(gr.GatherDate)
SELECT DeptID,
DeptSequence,
DeptName,
[getmoney1]=isnull(SUM(CASE gmm WHEN '1' THEN summon END),0),--列转行,n月份表示成getmoney[n]的形式
[getmoney2]=isnull(SUM(CASE gmm WHEN '2' THEN summon END),0),
[getmoney3]=isnull(SUM(CASE gmm WHEN '3' THEN summon END),0),
[getmoney4]=isnull(SUM(CASE gmm WHEN '4' THEN summon END),0),
[getmoney5]=isnull(SUM(CASE gmm WHEN '5' THEN summon END),0),
[getmoney6]=isnull(SUM(CASE gmm WHEN '6' THEN summon END),0),
[getmoney7]=isnull(SUM(CASE gmm WHEN '7' THEN summon END),0),
[getmoney8]=isnull(SUM(CASE gmm WHEN '8' THEN summon END),0),
[getmoney9]=isnull(SUM(CASE gmm WHEN '9' THEN summon END),0),
[getmoney10]=isnull(SUM(CASE gmm WHEN '10' THEN summon END),0),
[getmoney11]=isnull(SUM(CASE gmm WHEN '11' THEN summon END),0),
[getmoney12]=isnull(SUM(CASE gmm WHEN '12' THEN summon END),0)
FROM #temp
GROUP BY DeptID,DeptSequence,DeptName
order by DeptSequence for xml raw
drop table #temp,table1
end
GO
分享到:
相关推荐
这几天在写HRM的时候 这问题搞了我两天,开始在使用Google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了的msdn.yesky.com 找到点启示。 好了废话多说无用。 首先表结构如下 表名 Test 写个存储过程 ...
**详细解释:**题目给出了五个事件,分别是立遗嘱、建立公司、搞发明、设立基金和创财产。合理的顺序应该是先有发明创造,然后通过发明创造成立公司,进一步积累财富,最终设立基金并立遗嘱。因此,最合理的排序是**...
描述中提到的“U盘搞了两天终于搞好了”,暗示了用户在尝试修复U盘时可能遇到了一些困难,经过长时间的努力,最终通过这款特定的修复工具解决了问题。其中,“6699”可能是一个错误代码或者序列号,而“04752”同样...
根据给定文件的信息,我们可以提炼出以下几个关键知识点: ### 1. 作者及作品背景 - **作者**:迈克尔·沃尔夫(Michael Wolff),美国著名记者、作家及媒体评论家。 - **代表作**: - 《电视是新的电视:数字...
"使用我这个保证能修复好,前几天我姐家的管家婆系统出现此问题,但是在网上找好多解决办法都没有解决了,大部人还要收费。有个数据修复着问我要80RMB。我没有给他,我自己是搞网络的对数据库也不太了解,这两天上班...
的,所以我们可以把系统分为以下几个模块:数据处理电路、显示电路、待测信 号产生电路、待测信号整形放大电路,电源电路。 2.2 主要开发工具和平台 2.2.1 原理图和印刷电路板图设计开发工具:PROTEL DXP Protel DXP...
17.2 文件存储管理 .217 17.3 读 写 文 件 .222 17.4 异步文件操作 .227 17.5 小 结 .234 第十八章 高 级 话 题 .235 18.1 注册表编程 .235 18.2 在 C #代码中调用 C++和 VB 编写的组件 .240 18.3 版 ...