简体   繁体   English

选择日期范围内的特定日期

[英]Selecting certain days in a date range

I have the query below which gives me the number of days that a student was absent. 我在下面的查询中给了我学生缺勤的天数。 DATEDIFF and DATEPART calculate the weekdays and holidays should not be counted as an absent day. DATEDIFF和DATEPART计算的工作日和节假日不应算作缺席日。 The absent days stored in studentTable in two fields which are fromDate and toDate. 在studentTable中,缺席天数存储在fromDate和toDate两个字段中。 So the absent days are in a date range. 因此,缺席日期在日期范围内。 If a student is one day absent, it is recorded 11/23/2015, 11/23/2015. 如果学生缺席一天,则会记录为11/23 / 2015、11 / 23/2015。 If a student is two days absent ,then 11/23/2015, 11/24/2015. 如果学生缺席两天,请于11/23/2015,11/24/2015。

DECLARE @startDate DATE SET @startDate = '20151121'
DECLARE @endDate DATE SET @endDate = '20151123'       

SELECT      
  a.studentName

  ,SUM(DATEDIFF(dd, fromDate, toDate) 
  - (DATEDIFF(wk, fromDate, toDate) * 2) 
  -CASE WHEN DATEPART(dw, fromDate) = 1 THEN 1 ELSE 0 END 
  +CASE WHEN DATEPART(dw, toDate) = 1 THEN 1 ELSE 0 END + 1 )- COUNT(h.holiday)
   AS totalAbsentDay        

  FROM studentTable a 
LEFT OUTER JOIN holiday h 
ON h.holiday < a.toDate and h.holiday > a.fromDate

WHERE a.fromDate = @startDate AND a.toDate = @endDate
GROUP BY a.studentName

The problem here is that when I try to declare a start and an end date, it does not give me the correct absent days. 这里的问题是,当我尝试声明开始日期和结束日期时,它没有给我正确的缺席天数。 For example, if a student is absent between 11/23/2015 and 11/26/2015 which is 4 days absent , and I declare start date 11/22/2015 and end date 11/27/2015, the query doesn't give me the result of 3. 例如,如果学生在2015年11月23日到2015年11月26日之间缺席(缺席4天),并且我声明开始日期为11/22/2015和结束日期为11/27/2015,则查询不会给我3的结果

This query below will work for given database scheme, may not the best solution because use three level of queries 以下查询适用于给定的数据库方案,可能不是最佳解决方案,因为使用三级查询

DECLARE @startDate DATE SET @startDate = '2016-02-05'
DECLARE @endDate DATE SET @endDate = '2016-02-20'       

SELECT
    studentName,
    SUM(AbsentDay) totalAbsentDay
FROM
(
    SELECT      
      a.studentName
      ,DATEDIFF(dd, fromDate, toDate) 
      - (DATEDIFF(wk, fromDate, toDate) * 2) 
      -CASE WHEN DATEPART(dw, fromDate) = 1 THEN 1 ELSE 0 END 
      +CASE WHEN DATEPART(dw, toDate) = 1 THEN 1 ELSE 0 END + 1 - COUNT(h.holiday)
       AS AbsentDay        
      FROM (
            SELECT
                studentName,-- Name,
                CASE WHEN fromDate<@startDate THEN @startDate ELSE fromDate END fromDate,
                CASE WHEN toDate>@endDate THEN @endDate ELSE toDate END toDate
            FROM 
                StudentTable S
            WHERE 
                S.toDate >= @startDate AND s.fromDate <= @endDate
        ) a 
        LEFT OUTER JOIN holiday h 
        ON h.holiday < a.toDate and h.holiday > a.fromDate

    GROUP BY studentName, fromDate, toDate
) B
GROUP BY studentName

For easier query and faster execution please consider to redesign studentTable to something like idStudent , AbsentDate ..just a suggestion.. 为了方便查询和更快的执行速度,请考虑重新设计studentTable喜欢的东西idStudentAbsentDate ..just建议..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM