当前位置: 首页 > 编程日记 > 正文

Sql Server统计报表案例

场景:查询人员指定年月工作量信息

USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  procedure [dbo].[GetWorkLoadMain]
@year int,
@month int,
@UserId varchar(50)
as
begindeclare @day varchar(50)set @day=CAST(@year as varchar)+'-'+RIGHT(('00'+cast(@month as varchar)),2)+'-01'declare @sql varchar(max)
set @sql=''select @sql=@sql+',['+Rtrim(ProjectName)+'] int NULL DEFAULT 0  '  from 
( select distinct ProjectName from ProRecord  where TypeCode=0 and year(InsertTime)=@year  and month(InsertTime)=@month  ) t
set @sql=stuff( @sql,1,1,'') create table  #temp (日期 varchar(50))declare @count intset @count=(select COUNT(1) from ProRecord  where TypeCode=0 and year(InsertTime)=@year  and month(InsertTime)=@month )if @count>0beginexec (' alter table  #temp   add  '+@sql+'')endinsert into #temp(日期)
select convert(varchar(10),dateadd(dd,number,convert(varchar(8),@day,120)+'01'),120) as time  
from master..spt_values   
where type='P' and   
dateadd(dd,number,convert(varchar(8),@day,120)+'01')<=dateadd(dd,-1,convert(varchar(8),dateadd(mm,1,@day),120)+'01')declare @日期  varchar(50)    
declare @ProjectName  varchar(50)     
declare @num int  
declare @ru1 varchar(50) 
if(ISNULL(@UserId,'')<>'') begindeclare mycursor cursor for select CONVERT(varchar(10),InsertTime,23) as 日期,ProjectName, count(1) as num from ProRecord where UserId= @UserId and TypeCode=0 and year(InsertTime)=@year  and month(InsertTime)=@month group by CONVERT(varchar(10),InsertTime,23) ,ProjectNameend
elsebegindeclare mycursor cursor for select CONVERT(varchar(10),InsertTime,23) as 日期,ProjectName, count(1) as num from ProRecord where TypeCode=0 and year(InsertTime)=@year  and month(InsertTime)=@month group by CONVERT(varchar(10),InsertTime,23) ,ProjectNameendopen mycursor      
fetch next from mycursor into @日期,  @ProjectName,@num
while (@@fetch_status=0)  
begin      set  @ru1 =Rtrim(@ProjectName) exec ('update #temp set ['+@ru1+']='+@num+'  where 日期='+''''+@日期+''''+'  ' )fetch next from mycursor into  @日期,  @ProjectName, @num
end  
close mycursor  DEALLOCATE mycursor   end declare @sqlnew varchar(8000)
set @sqlnew=''
select @sqlnew=@sqlnew+',sum(cast('+name+' as int)) as '+name from tempdb.dbo.syscolumns where id=object_id('tempdb.dbo.#temp') and name not in('日期') order by colid     --写不要汇总的列名exec ('select * from #temp  where 1=1  union all select ''合计'' as 日期'+@sqlnew+' from #temp  ')--exec [GetWorkLoadMain] 2017,6,''
--exec [GetWorkLoadMain] 2017,6,'admin'

呈现效果:

分析:

1.行转列

declare @sql varchar(max)
set @sql=''select @sql=@sql+',['+Rtrim(ProjectName)+'] int NULL DEFAULT 0 ' from 
( select distinct ProjectName from ProRecord where TypeCode=0 and year(InsertTime)=@year and month(InsertTime)=@month ) t
set @sql=stuff( @sql,1,1,'')create table #temp (日期 varchar(50))
exec (' alter table #temp add '+@sql+'')

2.查询当前日期所在月份所有日期

select convert(varchar(10),dateadd(dd,number,convert(varchar(8),'2017-06-01',120)+'01'),120) as time 
from master..spt_values 
where type='P' and 
dateadd(dd,number,convert(varchar(8),'2017-06-01',120)+'01')<=dateadd(dd,-1,convert(varchar(8),dateadd(mm,1,'2017-06-01'),120)+'01')

3.游标处理数据

declare @日期 varchar(50) 
declare @ProjectName varchar(50) 
declare @num int 
declare @ru1 varchar(50) 
declare mycursor cursor for select CONVERT(varchar(10),InsertTime,23) as 日期,ProjectName, count(1) as num from ProRecord where UserId= @UserId and TypeCode=0 and year(InsertTime)=@year and month(InsertTime)=@month group by CONVERT(varchar(10),InsertTime,23) ,ProjectName
open mycursor 
fetch next from mycursor into @日期, @ProjectName,@num
while (@@fetch_status=0) 
begin 
set @ru1 =Rtrim(@ProjectName) 
exec ('update #temp set ['+@ru1+']='+@num+' where 日期='+''''+@日期+''''+' ' )
fetch next from mycursor into @日期, @ProjectName, @num
end 
close mycursorDEALLOCATE mycursorend

4.对动态列临时表进行汇总查询

注意:对于临时表,查询需加前缀 tempdb

declare @sqlnew varchar(8000)
set @sqlnew=''
select @sqlnew=@sqlnew+',sum('+name+' )  as '+name from tempdb.dbo.syscolumns where id=object_id('tempdb.dbo.#temp') and name not in('日期') order by colid --写不要汇总的列名exec ('select ''合计'' as 日期'+@sqlnew+' from #temp ')

转载于:https://www.cnblogs.com/byfcumt/p/6929357.html

相关文章:

运行报表时提示输入用户名和密码

在AX2012运行报表是总是提示用户输入用户名和密码&#xff1a; 尝试输入登陆名和密码&#xff0c;点击查看报表&#xff0c;出现如下错误&#xff1a; 因为AX2012的报表使用的针对AX2012客制化的SSRS&#xff0c;而要求输入登录名和密码是SSRS报表的数据源设置导致的。在SSRS管…

CSS 文字,边框实现从左至右颜色渐变

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.文本从左至右颜色渐变 效果图&#xff1a; 2.边框从左至右颜色渐变 效果图&#xff1a; 实现代码&#xff1a; 1.文本从左至右颜色渐变实现代码&#xff1a; <!DOCTYPE html> <html>&l…

如何使用Create-React-App和自定义服务人员构建PWA

Note: This is not a primer on create-react-app or what a service worker is. This post assumes prior knowledge of both.注意&#xff1a;这不是create-react-app或服务工作者的入门。 这篇文章假定两者都有先验知识。 So, I recently had the opportunity to work on a…

inline-block空隙怎么解决

方法一&#xff1a;移除空格 元素间留白间距出现的原因就是标签段之间的空格&#xff0c;因此&#xff0c;去掉HTML中的空格&#xff0c;自然间距就木有了。考虑到代码可读性&#xff0c;显然连成一行的写法是不可取的&#xff0c;我们可以&#xff1a; <div class"spa…

php 网络请求 get请求和post请求

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 代码记录 <?php header(content-type:application:json;charsetutf8); header(Access-Control-Allow-Origin:*); //header(Access-Control-Allow-Methods:POST); header(Access-Control-Allow-He…

docker查看现有容器_如何使用Docker将现有应用程序推送到容器中

docker查看现有容器by Daniel Newton丹尼尔牛顿 如何使用Docker将现有应用程序推送到容器中 (How to shove an existing application into containers with Docker) I have finally got round to learning how to use Docker past the level of knowing what it is and does w…

巧妙使用Firebug插件,快速监控网站打开缓慢的原因

巧妙使用Firebug插件&#xff0c;快速监控网站打开缓慢的原因 原文 巧妙使用Firebug插件&#xff0c;快速监控网站打开缓慢的原因 很多用户会问&#xff0c;我的网站首页才50KB&#xff0c;打开网页用了近60秒才打开&#xff1f;如何解释&#xff1f; 用户抱怨服务器运行缓…

第二阶段第三次站立会议

昨天做了什么&#xff1a;写了部分购物车的功能 今天要干什么&#xff1a;修改后台代码的错误 遇到的困难&#xff1a;没有困难转载于:https://www.cnblogs.com/jingxiaopu/p/7109774.html

微信小程序生成小程序二维码 php 直接可以用

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 小程序需要先上线才能生成二维码 HTTP请求的效果图&#xff1a; 小程序展示的效果图&#xff1a; 小程序展示二维码源码&#xff1a; 请求二维码图片base64路径&#xff0c;点击预览图片 onLoad: func…

vue和react相同点_我在React和Vue中创建了相同的应用程序。 这是区别。

vue和react相同点by Sunil Sandhu由Sunil Sandhu 我在React和Vue中创建了相同的应用程序。 这是区别。 (I created the same app in React and Vue. Here are the differences.) Having used Vue at my current workplace, I had a fairly solid understanding of how it all …

Filter(过滤器)

一、Filter过滤器(重要)     Javaweb中的过滤器可以拦截所有访问web资源的请求或响应操作。 1、Filter快速入门     1.1、步骤:      1. 创建一个类实现Filter接口      2. 重写接口中方法 doFilter方法是真正过滤的。      3. 在web.xml文件中配置 …

css3实现3D立体翻转效果

1、在IE下无法显示翻转效果&#xff0c;火狐和谷歌可以 1 /*样式css*/2 3 .nav-menu li {4 display: inline;5 }6 .nav-menu li a {7 color: #fff;8 display: block;9 text-decoration: none;10 overflow: visible;11 line-height: 40px;12 font-…

Ant Design 入门-参照官方文档使用组件

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 先来一个按钮组件使用的对比,官方文档的(不能直接用)和实际能用的。 官网demo: import { Table, Divider, Tag } from antd;const columns = [{title: Name,dataIndex: name,key: name,render: text =…

如何用JavaScript的回调函数做出承诺

by Adham El Banhawy由Adham El Banhawy 如何用JavaScript的回调函数做出承诺 (How to make a Promise out of a Callback function in JavaScript) Back-end developers run into challenges all the time while building applications or testing code. As a developer who …

VMware里的linux系统里的命令行里会有bee的声音,要如何关掉

VMware里的linux系统里的命令行里会有bee的声音&#xff0c;要如何关掉 取消bell报警声的方法&#xff1a;登陆linux系统vi /etc/inputrc找到set bell-style none 将前面的&#xff03;去掉&#xff0c;之后重启系统即可解决声音问题若不见效可以通过下面的方式解决下bell-styl…

React-Todos

最近学完React的最基本概念&#xff0c;闲下来的时候就自己写了一个Todo-List的小应用。这里做个简略的说明&#xff0c;给想好好学React的新手看。 React-Todo 学习前提 这里我用了webpackb做了babel和JSX预处理和模块打包。所以对React和一些ES2015&#xff08;ES6&#xff0…

Ant Design 入门-引用自己命名的组件

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 自己创建的组件:代码 import { Table, Divider, Tag } from antd; import React, { Component } from react; export default class My_Table extends Component {render() {const columns = [{title: …

迷宫出路代码_如何在软件开发的迷宫中找到自己的出路

迷宫出路代码by Tim Kleier蒂姆克莱尔(Tim Kleier) 如何在软件开发的迷宫中找到自己的出路 (How to find your way through the corn maze of software development) The corn maze is one of my favorite challenges to tackle. It’s an unnerving experience, especially w…

打包 React 项目并在服务器运行。

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.找到项目根目录的package.json文件&#xff1a;如图&#xff1a; 2.打开cmd执行&#xff1a;npm run build 3.生成DIST文件夹。 4.把DIST文件放到服务器phpStudty根目录&#xff0c;访问index.html。…

一些有用的Python问题

1. 修改IDLE工作路径&#xff0c;在命令交互模式下输入如下指令&#xff1a; >>> import os >>> os.getcwd() #查看当前的工作路径 >>> os.chdir(E:\\Python\\Demo) #修改当前的工作路径 2.Python中 ImportError: cannot import name NUMPY_MKL 的…

Python核心编程笔记---- print

在仅用变量名时&#xff0c;输出的字符串是用单引号括起来的。这个是为了让非字符串对象也可能以字符的形式显示在屏幕上。 而print 函数打印出来的是变量的值。 print 调用的是str()方法。而仅用变量名时调用的是repr()方法。 证明&#xff1a;------------------------------…

latex 插图解释_大O符号-只需插图和视频即可解释

latex 插图解释Big O notation is used to communicate how fast an algorithm is. This can be important when evaluating other people’s algorithms, and when evaluating your own! In this article, I’ll explain what Big O notation is and give you a list of the m…

[YTU]_2002(C语言实验——单词统计)

Description 从键盘输入一行字符&#xff0c;统计其中单词的个数&#xff0c;各单词以空格分隔&#xff0c;且空格数可以是多个。Input 输入只有一行句子。仅有空格和英文字母构成。 Output 单词的个数。 Sample Input stable marriage problem Consists of Matching memb…

资本中国人物-金融

一、二、三、店、五、土地、七、八、玖、拾起、白、千、一万、一亿、元(圆)、角、支、零、整个。这是上图中我们经常要填写。问&#xff1a;什么是它用数词&#xff1f;想必很多人都不是很清楚&#xff01; 请看下面的两个相关的表数词&#xff1a; 1、数字化和大、小写数字对照…

Ant Design Pro 网络请求流程

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 在 Ant Design Pro 中&#xff0c;一个完整的前端 UI 交互到服务端处理流程是这样的&#xff1a; UI 组件交互操作&#xff1b; 调用 model 的 effect&#xff1b; 调用统一管理的 service 请求函数&a…

在Google Cloud Platform上持续部署Node.js

by Gautam Arora由Gautam Arora 在Google Cloud Platform上持续部署Node.js (Continuous Deployment for Node.js on the Google Cloud Platform) Google Cloud Platform (GCP) provides a host of options for Node developers to easily deploy our apps. Want a managed ho…

hdu-1108 最小公倍数

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid1108 题目类型&#xff1a; 数论 题意概括&#xff1a; 求两个数的最小公倍数 解题思路&#xff1a; 模拟 题目&#xff1a; 最小公倍数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O…

sql-schema与catalog

schema&#xff1a; 指的是说当偶create database caiceclb时&#xff0c;caiceclb就是一个schema catalog&#xff1a; 指的是所有的database目录&#xff0c;就像上图显示的那样&#xff0c;将MySQL原来的&#xff08;mysql,infomation_schema&#xff09;及后来新建的的data…

这是如何更好地利用JavaScript数组的方法

by pacdiv由pacdiv 这是如何更好地利用JavaScript数组的方法 (Here’s how you can make better use of JavaScript arrays) Quick read, I promise. Over the last few months, I noticed that the exact same four mistakes kept coming back through the pull requests I c…

07、C语言——函数

函数 1、函数定义 函数返回值类型 函数名&#xff08;形式参数列表&#xff09;      {         函数体&#xff1b;      } 注意&#xff1a; 定义有参函数时&#xff0c;形参的定义可以采用传统方式或现代方式两种 1&#xff09;传统方式&#xff1a; int …