zoukankan      html  css  js  c++  java
  • 时间段内月份统计, 查询结果循环

    USE [CommunityApp]
    GO
    /****** Object:  StoredProcedure [dbo].[sp_count_OwnerPayList]    Script Date: 03/04/2016 11:32:31 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:        <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:    <Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[sp_count_OwnerPayList]  
     @CommunityIDs int,--如果为0查出所有的,不为0则查指定小区
     @FeeName nvarchar(50),
      @StarTime as datetime,--开始时间
     @EndTime as datetime --结束时间
    AS
      declare @i int,
        @j int,
       @months nvarchar(50),--当前月
       @FeeMoney decimal(10, 2),
      @CommunityID int,
      @CommunityName nvarchar(50)
     
      
     DECLARE @tCommunity TABLE  --所有的小区
     (  
      CommunityID  int  ,
      CommunityName nvarchar(50)
      ,FlagID   TINYINT    
     ) 
      DECLARE @tYuefen TABLE  --所有的月份
     (  
      months nvarchar(50)
      ,FlagID   TINYINT    
     ) 
     DECLARE @tcount TABLE  
     (  
      CommunityName nvarchar(50),--小区
      Months   nvarchar(100),--月份
      FeeMoney  decimal(10, 2)  --缴费金额
     ) 
     
    BEGIN  
    --获得小区 star
    IF @CommunityIDs=0
    begin
     insert @tCommunity  select CommunityID,CommunityName,0 from CommunityBase.dbo.CommunityList
     end
     else
     begin
      insert @tCommunity  select CommunityID,CommunityName,0 from CommunityBase.dbo.CommunityList where CommunityID=@CommunityIDs
     end
    --获得小区 end
    
    --获得月份 star
    
    ;with t
    as
    (select @StarTime as y
     union all
     select  dateadd(m,1,y)from t where y <@EndTime)
     insert @tYuefen select  convert(varchar(7),y,120),0 from t
     
     
    --获得月份 end
    
     
     SET @i=1  
     WHILE( @i>=1)  ---第一次循环star
     BEGIN  
        
      set @CommunityID=''  
       set @CommunityName=''  
      SELECT TOP 1 @CommunityID = CommunityID,@CommunityName = CommunityName FROM @tCommunity  WHERE flagID=0  
      SET @i=@@ROWCOUNT  
        
      IF @i<=0 GOTO Return_Lab  
         
     
     SET @j= 1
     WHILE( @j>=1) ---第二次循环star 
      BEGIN
         SELECT TOP 1 @months = months FROM @tYuefen  WHERE flagID=0  
      SET @j=@@ROWCOUNT  
      IF @j<=0 GOTO Return_Lab2  
      
      
    
       IF @FeeName=''
    begin
       SELECT @FeeMoney=sum(FeeMoney) FROM OwnerPayList WHERE PayMonth=@months  and CommunityID= @CommunityID   --月份的钱
     end
     else
     begin
       SELECT @FeeMoney=sum(FeeMoney) FROM OwnerPayList WHERE PayMonth=@months and FeeName=@FeeName and CommunityID= @CommunityID   --月份的钱
     end
       
     if @FeeMoney is not null
     begin
      insert @tcount values(@CommunityName,@months,@FeeMoney)
      end
      
       IF @@error=0   
          UPDATE @tYuefen SET flagID=1 WHERE months = @months
     
     
      Return_Lab2:    
       end ---第二次循环 end
       
       IF @@error=0   
          UPDATE @tCommunity SET flagID=1 WHERE CommunityID = @CommunityID
           
     Return_Lab:  
     END  ---第一次循环 end
     select * from   @tcount
    

  • 相关阅读:
    正则表达式---用户验证
    最短路 Dijkstra
    最短路 Dijkstra+堆优化
    LCIS(最长公共上升子序列)
    最小生成树 Kruskal
    网络流 最大流
    网络流 最小费用最大流
    树链剖分
    树状数组
    双重Hash
  • 原文地址:https://www.cnblogs.com/lucoo/p/5241481.html
Copyright © 2011-2022 走看看