SQL存储过程,主要产生交叉表,语句如下:

ALTER        PROCEDURE accident_report
   @areaCode varchar(20), --地区编码
   @totalYear int   --统计年度
AS
declare 
   @arealen int ,
   @areaCodeLike varchar(20) ,
   @thisRepMonth int
begin
create table #temp
(
 areaCode varchar(10),
 areaName varchar(30),
 accidentPlan int ,
 accidentNum int ,
 deadNum int,
 injuredNum int,
 rate decimal(10,4),
 lastYear int,
 compareRate decimal(10,4)
 )
insert into #temp
(areaCode,areaName )
select id,name from jaf_region where parentID=@areaCode
update #temp
set accidentPlan = b.accidentNum
from #temp a,accidentPlan b
where a.areaCode=b.areaCode and b.planYear=@totalYear
set @arealen = len(@areaCode)
set @areaCodeLike = @areaCode+'%'
select @thisRepMonth = max(repMonth )
from repAccident where repYear=@totalYear ;
update #temp set
accidentNum=b.accidentNum,
deadNum=b.deadNum,
injuredNum=b.injuredNum
from #temp ,(
select substring(place,1,@arealen+2) areaCode,count(id) accidentNum,sum(totalDead) deadNum ,
sum(totalGBH) injuredNum
from repaccident where repYear=@totalYear and place like @areaCodeLike
group by substring(place,1,@arealen+2)) b
where #temp.areaCode=b.areaCode
update #temp set
lastYear=b.accidentNum
from #temp ,(
select substring(place,1,@arealen+2) areaCode,count(id) accidentNum
from repaccident where repYear=@totalYear - 1 and repMonth <=@thisRepMonth
 and place like @areaCodeLike
group by substring(place,1,@arealen+2)) b
where #temp.areaCode=b.areaCode
update #temp set rate = accidentNum / accidentPlan where accidentPlan <>0
update #temp set compareRate = accidentNum / lastYear where lastYear <>0
update #temp set accidentPlan= 0 where accidentPlan is null
update #temp set accidentNum= 0 where accidentNum is null
update #temp set deadNum= 0 where deadNum is null
update #temp set injuredNum= 0 where injuredNum is null
update #temp set rate= 0 where rate is null
update #temp set lastYear= 0 where lastYear is null
update #temp set compareRate= 0 where compareRate is null

insert into #temp(areaCode,areaName,accidentPlan,accidentNum,deadNum,injuredNum,rate,lastYear,compareRate)
select '9999','合计',sum(accidentPlan),sum(accidentNum),sum(deadNum),sum(injuredNum),0,sum(lastYear),0
from #temp
update #temp set rate=accidentNum/accidentPlan
where accidentPlan <>0 and areaName='合计'
update #temp set compareRate=accidentNum/lastYear
where lastYear <>0 and areaName='合计'
select areaCode,areaName,accidentPlan,accidentNum,deadNum,injuredNum,rate,lastYear,compareRate
from #temp order by areaCode
end

转载于:https://blog.51cto.com/atdnian/124737

SQL 交叉表存储过程相关推荐

  1. mysql 交叉表 存储过程_用于生成交叉表的存储过程的存储过程

    以前总是在网上搜一些交叉表生成的相关代码 但是使用起来总是很复杂 看看 刚出炉的东西对你来说是不是有所帮助 :_) 先看看下一个生成的存储过程 :SQLServer2000 Create Proced ...

  2. mysql 交叉表 存储过程_SQL存储过程实现SPSS交叉表(有图有真相)

    SP代码: /****** Object: StoredProcedure [dbo].[Pro_CrossTable] Script Date: 03/27/2014 20:46:38 ****** ...

  3. [转]SQL交叉表实例

    /* 普通行列转换 (爱新觉罗.毓华 2007-11-18于海南三亚) 假设有张学生成绩表(tb)如下: Name Subject Result 张三 语文 74 张三 数学 83 张三 物理 93 ...

  4. MSSQLSERVER数据库- SQL交叉表

    /* 假设有张学生成绩表(tb)如下: Name Subject Result 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 李四 物理 94 */ --- ...

  5. 关于SQL数据表存储过程表名前缀换成dbo代码

    1.全部表名换成dbo <span style="font-size:18px;">EXEC sp_MSforeachtable 'exec sp_changeobje ...

  6. sql教师表存储过程

    编写一个PL/SQL块 ,输出字符串This a minimum anonymous block set severoutput on begin DBMS_OUTPUT.PUT_LINE(This ...

  7. SQL交叉表查询 如何实现

    请教大家一个有关SQL交驻报表查询问题,欢迎各位指教! 我想把图1的使用信息,使用SQL语句,实现如图2的结果. 表名 序号 字段名 a 1 c a 2 d a 3 e a 4 f a 5 g b 1 ...

  8. sql交叉报表实例(转)

    建表: 在查询分析器里运行: CREATE TABLE [Test] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [nvarchar] (50) CO ...

  9. sql行转列 列数据不定 sql交叉报表实例

    sql交叉报表实例 建表: 在查询分析器里运行: CREATE TABLE [Test] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [nvarcha ...

最新文章

  1. 【Linux】23.ubuntu定时执行脚本/etc/crontab 和 定时任务不执行的解决办法
  2. Leetcode No.122
  3. C#中线程的使用[Thread in C#]
  4. 如何配置SAP Analytics Cloud到SAP Cloud for Customer的连接
  5. 关于ICallbackEventHandler的疑问
  6. 前端学习(1268):axios的响应结果
  7. 更新显示当前歌曲的名称 winform 0130
  8. ZJOI2008 树的统计 树链剖分学习
  9. 6.边缘检测:梯度——回顾、简化的图像、边缘概念_1
  10. 梦幻内存辅助制作教程
  11. linux 移动硬盘位置,在linux中使用移动硬盘
  12. 用acdsee制作html,ACDSee 制作网络像册
  13. 在Linux中使用7zip/7zz
  14. QComboBox下拉框样式
  15. 由于您的系统没有安装html help,教你解决Chrome浏览器未安装flash控件方法
  16. Visio对一个对象进行水平翻转
  17. android的got表HOOK实现
  18. 广东:让“数据跑路”代替“民众跑腿”
  19. 走,上高架,看看Momenta“飞轮式L4”的“微操”技巧
  20. 深入分析ConcurrentHashMap的源码设计(中)-hash冲突

热门文章

  1. 歌谣舞台_高光时刻 || 哈油宝藏男孩沈家麒登上吉林卫视和广东卫视青春歌谣舞台!...
  2. 交叉编译 for arm-linux-gcc... no,QT4.8.6、tslib库移植到arm上配置出错!求大神指点
  3. 加油卡 格式判断 java_JAVA实验_题目8
  4. mysql和mongodb存储时间_MongoDB存储时间
  5. android 代码 lut,Android BitmapFactory.decodeResource()错误或用于加载大LUT的替代选项...
  6. Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…
  7. 时间戳转中国人能看得懂的日期格式 yy-mm-dd
  8. 【转】ArcGIS Server安装
  9. ComfortTypingPro快速录入工具
  10. wsl Java开发_WSL2开发环境搭建