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
分享到:
相关推荐
本文将从 Oracle 存储过程的基础知识开始,逐步深入到 Oracle 存储过程的高级应用,包括 Hibernate 调用 Oracle 存储过程和 Java 调用 Oracle 存储过程的方法。 Oracle 存储过程基础知识 Oracle 存储过程是 Oracle...
在IT行业中,数据库操作是日常开发中的重要环节,而存储过程是数据库中一种高效、封装性强的预编译语句集合。本问题涉及到的是在PowerBuilder(简称Pb)环境中如何调用Oracle或SQL Server等数据库中的存储过程。以下...
DB2存储过程是一种在数据库管理系统中预编译的SQL代码集合,它允许开发人员封装复杂的业务逻辑和数据处理操作,并可以被多次调用。DB2作为一款强大的关系型数据库管理系统,其存储过程功能强大,提高了应用程序的...
ORACLE 存储过程的异步调用 本文讨论了 ORACLE 存储过程的异步调用方法,旨在解决客户端长时间等待存储过程执行的问题。主要思路是使用 DBMS_JOB 包将主处理存储过程作为任务提交到任务队列中,并通过 DBMS_PIPE 包...
在使用解密存储过程时,需要注意以下几点: * 请确保您拥有足够的权限来访问和修改存储过程。 * 请确保您已经备份了数据库,以免在解密过程中出现错误。 * 请确保您已经了解解密存储过程的原理和实现机制,以免出现...
数据库查询的存储过程 数据库查询的存储过程是数据库管理系统中一种非常重要的概念。它可以将多个SQL语句组合成一个单元,提高数据库的查询效率和性能。 存储过程的优点: 1. 可以在单个存储过程中执行一系列SQL...
能不能写个动态的业务,只输入存储过程名称,自动获取存储过程参数,并且参数的数据从前台传递过来,这个就通用了。只写一个通用方法,就可以调用所有的存储过程。只根据输入不同的存储过程名称、参数内容,自动调用...
内容概要:简单的C# winform调用存储过程实例,创建存储过程入参,通过SqlConnection对象和SqlCommand对象调用存储过程,获取存储过程的出参并显示出来,详细代码注释,希望对用到C#调用存储过程的小伙伴有帮助 ...
创建MySQL存储过程通常涉及以下几个步骤: 1. 使用`CREATE PROCEDURE`语句定义存储过程的名称、输入/输出参数以及包含的SQL语句。 2. 在存储过程中,可以使用流程控制语句如`IF...ELSE`、`CASE`、`WHILE`和`LOOP`来...
使用SAP HANA存储过程有以下几个显著优点: 1. **减少网络和处理器负载**:对于数据密集型操作(如聚合),大量数据不必传输至应用服务器,从而减少了网络流量和CPU负荷。 2. **支持多结果集返回**:HANA存储过程...
SQL Server存储过程对比工具是一种高效且实用的软件解决方案,它专为数据库管理员和开发人员设计,用于比较和分析两个SQL Server数据库中的存储过程。这款工具的主要功能是帮助用户快速识别和定位不同数据库间存储...
资源名称:SQL_Server存储过程调试指南内容简介: 存储过程( Stored Procedure)是一组为了完成特定功能的 SQL 语句集,经编译后存储在数据库中。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来...
在Java编程中,调用数据库的存储过程是常见的任务,特别是在处理复杂的业务逻辑或需要高效数据操作时。本文将详细讲解如何在Java中调用含有`OUT`参数的存储过程,帮助开发者解决这类问题。 首先,理解存储过程的...
### INFORMIX存储过程手册概览 #### 存储过程概念与作用 存储过程是一种预编译的SQL脚本或程序,它驻留在数据库中,能够执行一系列复杂的数据库操作,如数据检索、更新、事务处理等。在Informix环境下,存储过程...
【存储过程的学习与提高】 存储过程是数据库管理系统中一种预编译的SQL语句集合,它允许用户在数据库中创建可重复使用的代码片段,用于执行特定任务。在SQL SERVER中,存储过程不仅可以提高代码的复用性和维护性,...
存储过程教程 目录 1.sql存储过程概述 2.SQL存储过程创建 3.sql存储过程及应用 4.各种存储过程使用指南 5.ASP中存储过程调用的两种方式及比较 6.SQL存储过程在.NET数据库中的应用 7.使用SQL存储过程要特别注意的...
使用`CREATE PROCEDURE`语句创建存储过程,需要注意以下几点: 1. **语句独立**:不能将`CREATE PROCEDURE`与其他SQL语句放在同一个批处理中。 2. **权限**:默认情况下,只有数据库所有者有创建存储过程的权限,但...
### DB2存储过程开发知识点详解 #### 一、DB2存储过程概述 DB2是IBM公司推出的一款关系型数据库管理系统,广泛应用于金融、电信等行业。存储过程是在数据库中存储的一组预编译的SQL语句和流程控制指令,用于执行...
sql Server 通用分页存储过程 sql Server 通用分页存储过程 sql Server 通用分页存储过程 sql Server 通用分页存储过程
实验六的目的是深入理解并掌握SQL Server中的存储过程和触发器。存储过程是预编译的SQL语句集合,它们可以被多次调用,提高了代码的重用性和执行效率,同时降低了网络流量。以下是关于存储过程和触发器的详细解释: ...