• sql 统计时间区间各区间出现的次数


    CREATE TABLE #temp
        (
          FName NVARCHAR(10) ,
          FStartDate DATETIME ,
          FEndDate DATETIME
        );
    
    INSERT  #temp
            ( FName, FStartDate, FEndDate )
    VALUES  ( '1#', '2022-04-12 08:00:00', '2022-04-12 17:00:00' )
    ,       ( '2#', '2022-04-12 08:00:00', '2022-04-12 13:00:00' )
    ,       ( '3#', '2022-04-12 12:00:00', '2022-04-12 17:00:00' )
    ,       ( '4#', '2022-04-12 08:00:00', '2022-04-12 17:00:00' );
    
    
    
    CREATE TABLE #tempdate
        (
          FName NVARCHAR(10) ,
          FStartDate DATETIME ,
          FEndDate DATETIME
        );
    
    DECLARE @FName NVARCHAR(20) ,
        @FStartDate DATETIME ,
        @FEndDate DATETIME;
    
    /* 声明游标,默认为FORWARD_ONLY游标 */
    DECLARE cur CURSOR
    FOR
        SELECT  *
        FROM    #temp;
    
     /* 打开游标 */
    OPEN cur;
    
    /* 读取第1行数据*/
    FETCH NEXT FROM cur INTO @FName, @FStartDate, @FEndDate; 
    WHILE @@FETCH_STATUS = 0 /* 用WHILE循环控制游标活动 */
        BEGIN
    	 
            WITH    qj
                      AS ( SELECT   FDate ,
                                    ROW_NUMBER() OVER ( ORDER BY t.FDate ) FIndex
                           FROM     ( SELECT    FStartDate FDate
                                      FROM      #temp
                                      WHERE     FStartDate >= @FStartDate
                                                AND FStartDate <= @FEndDate
                                      UNION ALL
                                      SELECT    FEndDate
                                      FROM      #temp
                                      WHERE     FEndDate >= @FStartDate
                                                AND FEndDate <= @FEndDate
                                    ) t
                           GROUP BY FDate
                         )
                INSERT  #tempdate
                        ( FName ,
                          FStartDate ,
                          FEndDate
                        )
                        SELECT  @FName ,
                                a.FDate ,
                                b.FDate
                        FROM    qj a
                                LEFT JOIN qj b ON b.FIndex = a.FIndex + 1
                        WHERE   b.FDate IS NOT NULL;
    
    	 /* 在循环体内将读取其余行数据 */
    		FETCH NEXT FROM cur INTO @FName, @FStartDate, @FEndDate; 
        END;
    
    /* 关闭游标 */
    CLOSE cur; 
    
    /* 删除游标 */
    DEALLOCATE cur; 
    
    SELECT * FROM #temp a
    LEFT JOIN #tempdate b ON b.FName=a.FName
    OUTER APPLY(SELECT COUNT(oa.FName) num FROM #temp oa WHERE oa.FName!=a.FName AND oa.FStartDate<=b.FStartDate AND oa.FEndDate >=b.FEndDate)o
    
    DROP TABLE #temp; 
    DROP TABLE  #tempdate
    

      

  • 相关阅读:
    【Caffe代码解析】Layer网络层
    Codeforces Round #276 (Div. 2)
    深入一点 让细节帮你和Fragment更熟络
    C-IDE使用指南
    14.11 隔离事务
    【工具类】遍历扫描目录下全部文件并列出
    Cocos2d-x源代码解析(1)——地图模块(1)
    【Android实战】----基于Retrofit实现多图片/文件、图文上传
    RabbitMQ基本使用
    Spring 4 中重定向RedirectAttributes的使用
  • 原文地址:https://www.cnblogs.com/miaololi/p/16133878.html
Copyright © 2020-2023  润新知