我要从这些日期中获取两个日期,分别为2019/07/26和2019/08/08
Year Month ActualDays WorkingDays
------------------------------------
2019 07 06 04
2019 08 08 06
DECLARE @start DATETIME, @end DATETIME
SET @start = '2019-07-26'
SET @end = '2019-08-08'
;WITH c(d) AS
(
SELECT TOP (DATEDIFF(DAY, @start, @end) + 1 )
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, @start)
FROM
sys.all_columns
)
SELECT
YEAR(t.[date]), MONTH(t.[date]), t.[days]
FROM
(SELECT
[date] = DATEADD(MONTH, DATEDIFF(MONTH, 0, d), 0),
[days] = COUNT(*)
FROM
c
GROUP BY
DATEDIFF(MONTH, 0, d)
UNION ALL
SELECT
d, NULL
FROM
c) AS t
WHERE
t.[days] IS NOT NULL
由此,我将按月和按年分组,需要工作日
答案 0 :(得分:0)
您可以试试这个-
DECLARE @startdate DATE = '20190701'
, @enddate DATE = '20190716'
DECLARE @temp TABLE(thedate DATE, DoCount INT);
WITH CTE (thedate) AS
(
SELECT @startdate
UNION ALL
SELECT DATEADD(day, 1, thedate)
FROM CTE
WHERE thedate < @enddate
)
INSERT INTO @temp
SELECT thedate,CASE WHEN DATEPART(dw, [thedate]) IN (1,7) THEN 0 ELSE 1 END
FROM CTE
SELECT YEAR(thedate),MONTH(thedate),
COUNT(DoCount) AS ActualDays,
SUM(DoCount) WorkingDays
FROM @temp
GROUP BY YEAR(thedate),
MONTH(thedate)