时间表内的 datediff
datediff inside the time schedule
我需要在某些类型的活动之间计算 datedif(ss, start_time, end_time),我很清楚该怎么做。我的代码是:
WITH WorkDates AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY A.NUMBER ORDER BY datestamp DESC) AS RowNumber,
p2.affected_item as Service,
p1.close_time as close_time,
datestamp,
description,
a.type,
a.number as number
from ACTIVITYM1 a
left outer join probsummarym1 p1 on a.number = p1.number
inner join probsummarym2 p2 on p1.number = p2.number
where ((a.type like 'status%'
and (description like '%to Work In Progress%'
or description like '%from Work In Progress%')) or a.type like 'Closed')
)
SELECT
O1.Service,
O1.number,
O1.close_time,
sum(DATEDIFF(ss, O2.datestamp, O1.datestamp)) AS sum_of_time
FROM WorkDates O1
LEFT JOIN WorkDates O2
ON O2.RowNumber = O1.RowNumber + 1
and O2.number = O1.number
where O2.description like '%to Work In Progress%'
and (O1.description like '%from Work In Progress%' or O1.type like 'Closed')
group by O1.Service, O1.number, O1.close_time
order by O1.Service, O1.number
我是这样做的,因为我需要在某些类型的活动之间计算它,而许多这样的 'time windows' 应该计算的地方并不是常数。而且它工作得很好,但它计算全职,包括假期,周末
但是我还需要在这里完成一件事。
我想计算 datediff(ss, start_date, end_date) 但仅限于周一至周五。这意味着,例如,如果 start_date 是星期五的 16:00 和 end_date 星期一的 7:00,datediff 应该是 (24:00 - 16:00) + (07:00 - 00:00) = 15 小时。可能吗?
你应该计算周差,然后将其转换为秒,然后从 datediff 中扣除。
像这样:
sum(DATEDIFF(ss, O2.datestamp, O1.datestamp) - abs(DATEPART(ww,O2.datestamp) - DATEPART(ww,O1.datestamp)) * 2 * 24 * 60 * 60 ) AS sum_of_time
我需要在某些类型的活动之间计算 datedif(ss, start_time, end_time),我很清楚该怎么做。我的代码是:
WITH WorkDates AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY A.NUMBER ORDER BY datestamp DESC) AS RowNumber,
p2.affected_item as Service,
p1.close_time as close_time,
datestamp,
description,
a.type,
a.number as number
from ACTIVITYM1 a
left outer join probsummarym1 p1 on a.number = p1.number
inner join probsummarym2 p2 on p1.number = p2.number
where ((a.type like 'status%'
and (description like '%to Work In Progress%'
or description like '%from Work In Progress%')) or a.type like 'Closed')
)
SELECT
O1.Service,
O1.number,
O1.close_time,
sum(DATEDIFF(ss, O2.datestamp, O1.datestamp)) AS sum_of_time
FROM WorkDates O1
LEFT JOIN WorkDates O2
ON O2.RowNumber = O1.RowNumber + 1
and O2.number = O1.number
where O2.description like '%to Work In Progress%'
and (O1.description like '%from Work In Progress%' or O1.type like 'Closed')
group by O1.Service, O1.number, O1.close_time
order by O1.Service, O1.number
我是这样做的,因为我需要在某些类型的活动之间计算它,而许多这样的 'time windows' 应该计算的地方并不是常数。而且它工作得很好,但它计算全职,包括假期,周末
但是我还需要在这里完成一件事。
我想计算 datediff(ss, start_date, end_date) 但仅限于周一至周五。这意味着,例如,如果 start_date 是星期五的 16:00 和 end_date 星期一的 7:00,datediff 应该是 (24:00 - 16:00) + (07:00 - 00:00) = 15 小时。可能吗?
你应该计算周差,然后将其转换为秒,然后从 datediff 中扣除。 像这样:
sum(DATEDIFF(ss, O2.datestamp, O1.datestamp) - abs(DATEPART(ww,O2.datestamp) - DATEPART(ww,O1.datestamp)) * 2 * 24 * 60 * 60 ) AS sum_of_time