首页 > Sql Server统计报表案例

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

更多相关:

  • ---修改---create proc p_update ( @ID int, @Name varchar(50), @Age varchar(50), @Sex varchar(20), @StatId int )asbeginupdate Stu set Name=@Name,Age=@Age,Sex=@Sex,StatId=@S...

  • SQL获取当月天数的几种方法 原文:SQL获取当月天数的几种方法 日期直接减去int类型的数字 等于 DATEADD(DAY,- 数字,日期) 下面三种方法: 1,日期加一个月减去当前天数,相当于这个月最后一天的日期。然后获取天数。(注意,不能用这种方式:当前日期减去当前天数,再加一个月,新日期不一定是当前月的最后一天的日期...

  • 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类。这个类最主要的作用就是获取当前时间,我们来看下 Date 类的使用: 使用 Date 类的默认无参构造方法创建出的对象就代表当前时间,我们可以直接输出 Date 对象显示当前的时间,显示的结果如下: 其中, Wed 代表...

  • MySQL Date 函数 函数描述NOW()返回当前的日期和时间CURDATE()返回当前的日期CURTIME()返回当前的时间DATE()提取日期或日期/时间表达式的日期部分EXTRACT()返回日期/时间按的单独部分DATE_ADD()给日期添加指定的时间间隔DATE_SUB()从日期减去指定的时间间隔DATEDIFF()返回两...

  • 日期类NSDate,存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间方法description字符串以GMT=0展示日期如:2011-11-16 07:02:25 +0000测试的北京时间:2011-11-16 15:02:25.324/))))((((/格式化日期类型,使用NSDateFormatter该格式类常用方法...

  •   2007-09-03 15:29:51|  分类: SQL2000数据库 |字号 订阅 1.一个月第一天的 Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 2.本周的星期一 Select DATEADD(wk, DATEDIFF(wk,0,getdate()),...

  • 数据分析过程中,我们经常可以看到提数的SQL语句,了解SQL常用的基础查询语句,是检验提数逻辑是否正确的途径之一,并且也能更方便使用SMART BI数据分析工具。今天就让小编带大家走进SQL基础查询的世界吧~1、查询单个字段:语法:SELECT 字段名 FROM 表名举例:SELECT first_name FROM employ...

  •   SELECT * FROM tableSELECT * FROM table WHERE name = '强哥'SELECT * FROM table ORDER BY updateTime DESC...

  • 使用 OpenRowSet 和 OpenDataSource 访问 Excel 97-2007 测试文件:D:97-2003.xls和D:2007.xlsx,两个文件的内容是一模一样的。 测试环境:SQL Server 2000 / 2005。 -------------------------------------------...

  • exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure SELECT * INTO tmp_asset FROM OPENROWSET('Microsof...

  • select b.*,(select count(a.id) from td_product a where a.protypeid=b.id) num from td_protype b 转载于:https://www.cnblogs.com/shanlin/archive/2011/09/27/2192725.html...