【问题标题】:How do I get count of weekend days from a range of dates如何从一系列日期中计算周末天数
【发布时间】:2015-02-03 06:50:24
【问题描述】:

我必须找出开始日期和结束日期之间的星期六和星期日的总数。

示例 #1:

StartDate = Getdate(), EndDate = GetDate() + 5      -- result should be 2.

示例 #2:

StartDate = Getdate(), EndDate = GetDate() + 10     -- result should be 4.

谁能推荐一下。

【问题讨论】:

    标签: sql sql-server-2008 date select dayofweek


    【解决方案1】:

    你可以使用 Datepart http://msdn.microsoft.com/en-us/library/ms174420.aspx

    一个例子;

    WITH CTE as(
    Select DATEPART(WeekDay,MyDate) as DP From Table Where Mydate > @StartDate and MyDate < @EndDate)
    Select Count(*) as CT,DP From CTE
    group by DP
    

    星期六是 7,星期日是 1,所以您可以查看它们旁边的计数。

    【讨论】:

      【解决方案2】:

      试试这个:

      declare @startdate datetime = getdate()
      declare @days int = 5
      declare @cal table(dt datetime)
      
      declare @counter int = 0
      while @counter < @days
         begin
         insert into @cal values (@startdate + @counter) --Ideally should be dateadd(dd,@counter,@startdate)
         set @counter = @counter + 1
      end
      
      select count(*) from @cal
      where datename(dw,dt) = 'Saturday' or datename(dw,dt) = 'Sunday'
      --Ideally should be 
      --where datename(dw,dt) = 1 or datename(dw,dt) = 7
      

      Demo

      我们正在做的是建立从您的开始日期到结束日期的天数列表,然后计算从这些日期开始的周末。一个表变量用于存储这个列表。

      这里需要注意的2点:

      1. 理想情况下,您应该使用dateadd 函数而不是+ 运算符来执行日期时间计算。
      2. 虽然为了清楚起见,我使用了datename,但datepart 会更好,因为它提供数值,而datename 提供与语言相关的值。

      【讨论】:

        【解决方案3】:

        试试这个:

        DECLARE @V_StartDate DATETIME = GETDATE(), @V_EndDate DATETIME = GETDATE() + 5;
        
        WITH showDateCTE(DateCol)
        AS
        (
        SELECT DateCol = @V_StartDate
        UNION ALL
        SELECT DATEADD(DAY, 1, DateCol) 
        FROM showDateCTE 
        WHERE DateCol < @V_EndDate - 1
        )
        SELECT COUNT(1) weekEndCount  
        FROM showDateCTE  
        WHERE DATENAME(dw, CONVERT(DATE, DateCol)) IN ('Saturday', 'Sunday');
        

        【讨论】:

          【解决方案4】:
              declare @startdate datetime 
              declare @enddate datetime
              declare @weekendCnt int
              set @startdate = getdate()
              set @enddate = getdate()+8
              set @weekendCnt = 0
          
              while @startdate < @enddate
              begin
              PRINT @startdate
               if(datename(dw, @startdate) in( 'Saturday','Sunday'))
                   begin
                       set @weekendCnt = @weekendCnt + 1         
                   end
                   set @startdate = @startdate +1
               end
              print @weekendCnt
          

          【讨论】:

            【解决方案5】:

            这里是

            DECLARE @STARTDATE DATE='01/JAN/2014'    
            DECLARE @ENDDATE DATE='01/MAR/2014'
            
            ;WITH  CTE as
            (
                SELECT  CAST(@STARTDATE AS DATE) as [DAYS] 
                UNION ALL
                SELECT DATEADD(DAY,1,[DAYS]) [DAYS]
                FROM    CTE
                WHERE   [DAYS] < CAST(@ENDDATE AS DATE)
            )
            SELECT DISTINCT COUNT([DAYS]) OVER(PARTITION BY DATENAME(WEEKDAY,[DAYS])) CNT,
            DATENAME(WEEKDAY,[DAYS]) WD
            FROM CTE 
            WHERE DATENAME(WEEKDAY,[DAYS]) = 'SATURDAY' OR DATENAME(WEEKDAY,[DAYS]) = 'SUNDAY'
            ORDER BY DATENAME(WEEKDAY,[DAYS]) 
            

            这是你的结果

            【讨论】:

              【解决方案6】:

              今天有同样的问题。我到了这里。

              如果您不想使用递归 (CTE) 或 while。您可以使用数学加大小写:

              DECLARE @StartDate AS DATE
              DECLARE @EndDate AS DATE
              SET @StartDate = Getdate()
              SET @EndDate = GetDate() + 11
              
              SELECT
                  -- Full WE (*2 to get num of days Sa and So)             
                  (((DATEDIFF(d,@StartDate,@EndDate)+1)/7)*2)
                  +
                  -- WE-Days in between; given that Saturday = 7 AND Sunday = 1
                  -- what if startdate is sunday And you have remaining Days; you will always only get one WE-day
                  CASE WHEN DATEPART(dw,@StartDate) = 1 AND (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 THEN 1
                      -- If you have remaining days (Modulo 7 > 0) and the sum of number of starting day and remaining days is 8 (+1 for startingdate) then you have + 1 WE-day (its a saturday)  
                      ELSE CASE WHEN (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 AND (((DATEDIFF(d,@StartDate,@EndDate)+1)%7) + DATEPART(dw,@StartDate)) = 8 THEN 1
                          -- If the remaining days + the number of the weekday is are greater then 8 (+1 for startingdate) you have 2 days of the weekend in between.  
                          ELSE CASE WHEN (DATEDIFF(d,@StartDate,@EndDate)+1)%7 > 0 AND (((DATEDIFF(d,@StartDate,@EndDate)+1)%7) + DATEPART(dw,@StartDate)) > 8 THEN 2
                          -- you have no WE-days in between! Either because of the fact that you have a number that is divisable by 7 or because the remaining days are between 2 (Tuesday) and 6 (Friday)   
                          ELSE 0
                          END
                      END
                  END   AS TotalWEDays
              

              我希望 cmets 清楚这一点。如果有帮助,请告诉我。

              【讨论】:

                猜你喜欢
                • 2014-02-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-05-02
                • 2014-10-31
                • 2020-08-21
                • 2010-09-22
                • 1970-01-01
                相关资源
                最近更新 更多