我怎么知道服务器DECLARE @TABLE TABLE (Date1 DATETIME, Date2 DATETIME); -- Days field doesn't matter.
INSERT @TABLE(Date1, Date2) VALUES
('2017-01-25 15:00:00.000', '2017-02-03 03:00:00.000');
WITH CTE1(N) AS (SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) A(B)),
CTE2(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1 FROM CTE1 A CROSS JOIN CTE1 B CROSS JOIN CTE1) -- Generate numbers 0-999.
SELECT Periode = DATENAME(MONTH, D.Date1),
Days = SUM(DATEDIFF(HOUR, D.Date1, D.Date2)) / 24.0 -- Total hours in month / 24.0 for the number of days.
FROM @TABLE T
CROSS APPLY -- Calculate the start and end of each day within a the period.
(
SELECT Date1 = CASE C.N WHEN 0 THEN T.Date1 ELSE DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(DAY, C.N, T.Date1)), 0) END, -- Start of each day.
Date2 = CASE WHEN C.N = DATEDIFF(DAY, T.Date1, T.Date2) THEN T.Date2 ELSE DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(DAY, C.N, T.Date1)), 1) END -- End of each day.
FROM CTE2 C
WHERE C.N <= DATEDIFF(DAY, T.Date1, T.Date2)
) D
GROUP BY DATENAME(MONTH, D.Date1), DATEPART(MONTH, D.Date1)
ORDER BY DATEPART(MONTH, D.Date1)
;
正常运行时间?在某些情况下,如果机器没有使用50分钟,我会想关机。我怎么能这样做?