简体   繁体   English

SQL - 从工作天数中查找工作日

[英]SQL - Finding working days from days worked

We have a table that contains resources and the days' that they work.我们有一个表格,其中包含资源和它们工作的天数。 For example, one person could work Monday - Friday but another could only work two days of that week.例如,一个人可以在周一至周五工作,但另一个人只能在该周的两天工作。 Here is the data:这是数据:

WorkOnSunday    WorkOnMonday    WorkOnTuesday   WorkOnWednesday WorkOnThursday  WorkOnFriday    WorkOnSaturday
--------------------------------------------------------------------------------------------------------------
0               1               1               1               1               1               0
0               0               1               1               0               0               0

(apologies for the lack of formatting but screenshots wont upload through our damn proxy.) (抱歉没有格式化,但截图不会通过我们该死的代理上传。)

So the question is, how do I get the amount of days worked in a month using the data above?所以问题是,如何使用上述数据获得一个月的工作天数? (Holidays are the next stage. I'm attempting to get a hold of the holidays table that we apparently have) (假期是下一阶段。我正试图掌握我们显然拥有的假期表)

Here is what I have so far:这是我到目前为止所拥有的:

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2017/12/01'
SET @EndDate = '2018/01/01'


SELECT
   (DATEDIFF(dd, @StartDate, @EndDate))
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)

This gives me the correct amount of weekdays in the month这给了我当月正确数量的工作日

you need to find out first how many of each day is occuring in your month,你需要先找出你一个月中每天发生了多少天,
the following query can help you with that.以下查询可以帮助您解决这个问题。

declare @from datetime= '2017/12/01' 
declare @to datetime  = '2018/01/01'

select 
 datediff(day, -7, @to)/7-datediff(day, -6, @from)/7 AS MON,
 datediff(day, -6, @to)/7-datediff(day, -5, @from)/7 AS TUE,
 datediff(day, -5, @to)/7-datediff(day, -4, @from)/7 AS WED,
 datediff(day, -4, @to)/7-datediff(day, -3, @from)/7 AS THU,
 datediff(day, -3, @to)/7-datediff(day, -2, @from)/7 AS FRI,
 datediff(day, -2, @to)/7-datediff(day, -1, @from)/7 AS SAT,
 datediff(day, -1, @to)/7-datediff(day, 0, @from)/7 AS SUN

Once you have that you can do your counts in your table一旦你有了它,你就可以在你的表中进行计数

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

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