sql 统计报表存储过程

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go








ALTER PROCEDURE [dbo].[getBanJianTongJibaobiao1](
                                   @distictId INT, 
                                   @deId INT,
                                   @startdate VARCHAR(25),
                                   @dateEnd VARCHAR(25)
                                  )
 AS
------------------------------------------------------------------------
-- SQL 存储过程 办件汇总表 
    -- @distictId 区号
    -- deptId  部门号 
    -- windowNo  窗口号
    -- @startdate 开始时间
    -- @startdate 结束时间 
    -- typeContext 办件状态名称
    -- shouJian 收件数量
    -- banJian 办件数量
------------------------------------------------------------------------
BEGIN
       -- 声明变量
    DECLARE @typeContext_TMP VARCHAR(50);
    DECLARE @shouJian_TMP bigint ;
    DECLARE @banJian_TMP bigint ;
    DECLARE @ishouJian_TMP bigint ;
    DECLARE @ibanJian_TMP bigint ;
    DECLARE @ideptId bigint ;
    DECLARE @at_end INT ;
    DECLARE @Id1 bigint;	
    DECLARE @sbJian_TMP bigint ; 

    select @shouJian_TMP=0,@banJian_TMP=0,@ishouJian_TMP=0,@ibanJian_TMP=0,@ideptId=0,@at_end=0,@sbJian_TMP=0  
     --声明异常
     --DECLARE not_found CONDITION FOR SQLSTATE '02000';
    
    --定义临时表空间
     CREATE TABLE #SESSION_TEMP
	    ( 
	    	casecontext varchar(50),
			  shoujian  int,
			  banjian  int
	    )
     	  
		
    --1、求办件状态值
    DECLARE cursor1 CURSOR  FOR   
        select dictValue,dicId from T_DICTIONAY
             where dictTypeId=73 ;   
    --DECLARE   cursor2 CURSOR  FOR 
        --select casecontext,shoujian,banjian from #SESSION_TEMP;	   
    --DECLARE CONTINUE HANDLER FOR not_found
        --SET @at_end = 1;
  --找出所属部门下的所有子部门包括自己
    
     open cursor1;    
     --open cursor2;			  
  
 fetch  next from cursor1 into @typeContext_TMP,@Id1; 
  --WHILE @at_end=0
  WHILE @@FETCH_STATUS = 0 
  BEGIN
    set @shouJian_TMP=0;
    set @banJian_TMP=0;
    set @ishouJian_TMP=0;
    set @ibanJian_TMP=0;
    if @distictId=0    --全部区
    begin
         if @deId=0   --全部部门
         begin
             --收件数
              select  @shouJian_TMP=isnull(sum(caseNumber),0)  from t_caseBaseInfo  
              where convert(varchar(10),caseStartDate,121)>=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),caseStartDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121) and caseTypeId=@Id1;
              --办件数    
              select @banJian_TMP=isnull(sum(caseNumber),0)  from t_caseBaseInfo a
              where convert(varchar(10),a.casePromiseDate,121)>=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),a.casePromiseDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121)
                    and a.caseTypeId=@Id1 and a.caseStatusId in(141,142,143,199) ;--已办结,已退回,已作废,已发证
         end
    end   
    else   --某个区
    begin
          if @deId=0  --所有部门
          begin
             select  @shouJian_TMP=isnull(sum(caseNumber),0)  from t_caseBaseInfo a
                   where convert(varchar(10),caseStartDate,121)>=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),caseStartDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121) 
                   and caseTypeId=@Id1
                   and a.caseDeptId in(select DEPTID from t_department where DISTID=@distictId and  enable='启用' and ifreport='是');
    
             select @banJian_TMP=isnull(sum(caseNumber),0)  from t_caseBaseInfo a
                   where convert(varchar(10),a.casePromiseDate,121)>=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),a.casePromiseDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121)
                   and a.caseTypeId=@Id1 and a.caseStatusId in(141,142,143,199) --已办结,已退回,已作废,已发证
                   and a.caseDeptId in(select DEPTID from t_department where DISTID=@distictId and enable='启用' and ifreport='是');
          end
          else   --某个区某个部门
          begin
			DECLARE R_CRSR2 CURSOR  FOR 
			    with t(i)as(select deptid from t_department where deptid=@deId and enable='启用' and ifreport='是' 
			    union all 
				select d.deptid from t_department d,t
				where t.i=d.parentid and d.enable='启用' and d.ifreport='是')
				select i from t;
           OPEN R_CRSR2;
           FETCH next from R_CRSR2  INTO @ideptId;
			   --WHILE @at_end=0
               WHILE @@FETCH_STATUS = 0 
			   begin
					--set @shouJian_TMP=0;
					--set @banJian_TMP=0;
					set @ishouJian_TMP=0;
					set @ibanJian_TMP=0;
					set @sbJian_TMP=0;
				 set @ishouJian_TMP=(select isnull(sum(caseNumber),0)  from t_caseBaseInfo 
				    where convert(varchar(10),caseStartDate,121)>=convert(varchar(10),cast(@startdate as datetime),121) 
					and convert(varchar(10),caseStartDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121) 
					and caseTypeId=@Id1 and CASEDEPTID=@ideptId);
	                 
				 set @shouJian_TMP=@ishouJian_TMP+@shouJian_TMP;
	             
       /*
		Select @jibanjian1 = isNull(Sum(CASENUMBER),0)
            from t_casebaseinfo a
            where a.CASETYPEID = 151 			
			and a.CASEDEPTID  = @DeNo 
			and a.CASESTATUSID in (141,143,199)
            and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121)
			and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) 
		*/
			
				 set @ibanJian_TMP=(select isnull(sum(caseNumber),0)  from t_caseBaseInfo a 
					where  convert(varchar(10),a.casePromiseDate,121)>=convert(varchar(10),cast(@startdate as datetime),121)
					and convert(varchar(10),a.casePromiseDate,121)<=convert(varchar(10),cast(@dateEnd as datetime),121) 
					and a.caseStatusId in(141,142,143,199) --已办结,142已退回,已作废,已发证
					and a.caseTypeId=@Id1 and CASEDEPTID=@ideptId) ;
				 set @banJian_TMP=@ibanJian_TMP+@banJian_TMP;
	     
				 FETCH next from R_CRSR2  INTO @ideptId;
			   end;
           CLOSE R_CRSR2
           deallocate R_CRSR2
          end
      end
   
    insert into #SESSION_TEMP values(@typeContext_TMP,@shouJian_TMP,@banJian_TMP)
    --set @at_end=0;
    fetch  next from cursor1 into @typeContext_TMP,@Id1;
 END
 CLOSE cursor1
 deallocate cursor1

--add 咨询件统计
set @typeContext_TMP = '咨询件';
if @distictId=0    --全部区
    begin
         if @deId=0   --全部部门
			begin
				select @shouJian_TMP=count(*),@banJian_TMP=count(*) from t_sendno,T_ADVISORY where sendno=advicaseno  
					and convert(varchar(10),senddate,121) >=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),senddate,121) <= convert(varchar(10),cast(@dateEnd as datetime),121)
			end
	end   
 else   --某个区
    begin
          if @deId=0  --所有部门
			begin
				select @shouJian_TMP=count(*),@banJian_TMP=count(*) from t_sendno,T_ADVISORY where sendno=advicaseno 
					and convert(varchar(10),senddate,121) >=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),senddate,121) <= convert(varchar(10),cast(@dateEnd as datetime),121)
					and senddeptid in(select deptid from t_department where distid = @distictId)
			end
          else   --某个区某个部门
			begin
				select @shouJian_TMP=count(*),@banJian_TMP=count(*) from t_sendno,T_ADVISORY where sendno=advicaseno 
					and convert(varchar(10),senddate,121) >=convert(varchar(10),cast(@startdate as datetime),121) and convert(varchar(10),senddate,121) <= convert(varchar(10),cast(@dateEnd as datetime),121)
					and senddeptid in(@deId);
			end
      end
				
 insert into #SESSION_TEMP values(@typeContext_TMP,@shouJian_TMP,@banJian_TMP)
 
 SELECT casecontext,shoujian,banjian FROM #SESSION_TEMP;
 DROP table #SESSION_TEMP;
 
END

            
                  









 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go --drop PROCEDURE getAllBanJianTongJiData ALTER PROCEDURE [dbo].[getAllBanJianTongJiData] @p_district_no int, @StartDate datetime, @EndDate datetime AS set nocount on Declare @Name char(50) Declare @jibanjian bigint Declare @jibanjian1 bigint Declare @tuihuijian bigint Declare @bubanjian1 bigint Declare @bubanjian2 bigint Declare @chengnuojian1 bigint Declare @chengnuojian2 bigint Declare @chengnuojian3 bigint Declare @chengnuojian4 bigint Declare @lianbanjian1 bigint Declare @lianbanjian2 bigint Declare @shangbaojian1 bigint Declare @shangbaojian2 bigint Declare @DeNo bigint Declare @sumbj bigint Declare @sumbj1 bigint declare @huzu1 bigint declare @huzu2 bigint CREATE TABLE #t ( Name char(50), jibanjian bigint, jibanjian1 bigint, tuihuijian bigint, bubanjian1 bigint, bubanjian2 bigint, chengnuojian1 bigint, chengnuojian2 bigint, chengnuojian3 bigint, chengnuojian4 bigint, lianbanjian1 bigint, lianbanjian2 bigint, shangbaojian1 bigint, shangbaojian2 bigint, sumbj bigint, sumbj1 bigint, huzu1 bigint, huzu2 bigint ) begin Declare My_CURSOR CURSOR FOR Select DEPTID from t_department where ENABLE= '启用' and DISTID = 1 and deptType=1 and deptid not in (243,244)--and is_convenience=@tjType jc_dreaming Order by DEPTCODE /**ONLY VALID DEPARTMENT */ open My_CURSOR end FETCH NEXT FROM My_CURSOR INTO @DeNo while (@@FETCH_STATUS = 0) BEGIN Select @Name = DEPTNAME from t_department where DEPTID = @DeNo and deptType=1 and deptid not in (243,244) Select @jibanjian = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 151--instance of 1 and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @jibanjian1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 151 and a.CASEDEPTID = @DeNo and a.CASESTATUSID in ((141,142,143,199) and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @tuihuijian = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 203--instance of3 and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @bubanjian1 =isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 202 and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @bubanjian2 =isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 202 and a.CASESTATUSID in (141,142,143,199) and a.CASEDEPTID = @DeNo and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) ---CASEPROMISEDATE is not null Select @chengnuojian1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID =132 and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @chengnuojian2 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID =132 and a.CASESTATUSID in (141,142,143,199) and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @chengnuojian3 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a,t_CaseBusinessInfo b where a.CASETYPEID = 132 and a.caseid=b.busiCaseId and b.BUSIOPERATE = 3 and a.CASESTATUSID in (141,142,143,199) and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) and convert(varchar(10),a.casePromiseDate,120)>=convert(varchar(10),b.BUSIOPERATEDATE,120) Select @chengnuojian4 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a, t_CaseBusinessInfo b where a.CASETYPEID = 132 and a.caseid=b.busiCaseId and b.BUSIOPERATE = 3 and a.CASESTATUSID in (141,142,143,199) and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) and convert(varchar(10),a.casePromiseDate,120)<convert(varchar(10),b.BUSIOPERATEDATE,120) Select @lianbanjian1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 134 and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @lianbanjian2 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 134 and a.CASEDEPTID = @DeNo and a.CASESTATUSID in(2,4) --++++++++++++++++++++ and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @shangbaojian1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASETYPEID = 133 and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @shangbaojian2 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where CASETYPEID = 133 and CASESTATUSID in (141,143,199) and a.CASEDEPTID = @DeNo and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @sumbj = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASEDEPTID = @DeNo and convert(varchar(10),a.casePromiseDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),a.casePromiseDate,121)<=convert(varchar(10),cast(@endDate as datetime),121) Select @sumbj1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASEDEPTID = @DeNo and casestatusid in (141,142,143,199) and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast(@startDate as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @huzu1 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast('2009-10-08' as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Select @huzu2 = isNull(Sum(CASENUMBER),0) from t_casebaseinfo a where casestatusid in (141,142,143,199) and a.CASEDEPTID = @DeNo and convert(varchar(10),caseStartDate,121)>= convert(varchar(10),cast('2009-10-08' as datetime),121) and convert(varchar(10),caseStartDate,121) <=convert(varchar(10),cast(@endDate as datetime),121) Insert Into #t (Name,jibanjian,jibanjian1,tuihuijian,bubanjian1,bubanjian2, chengnuojian1,chengnuojian2,chengnuojian3,chengnuojian4, lianbanjian1,lianbanjian2,shangbaojian1,shangbaojian2,sumbj,sumbj1,huzu1,huzu2) Values(@Name,@jibanjian,@jibanjian1,@tuihuijian,@bubanjian1,@bubanjian2, @chengnuojian1,@chengnuojian2,@chengnuojian3,@chengnuojian4, @lianbanjian1,@lianbanjian2,@shangbaojian1,@shangbaojian2,@sumbj,@sumbj1,@huzu1,@huzu2) FETCH NEXT FROM My_CURSOR INTO @DeNo END CLOSE My_CURSOR DEALLOCATE My_CURSOR select * from #t

 

你可能感兴趣的:(sql,咨询,Go)