Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Comment puis-je obtenir le nombre de jours de week-end à partir d'une plage de dates

Le voici

DECLARE @STARTDATE DATE='01/JAN/2014'    
DECLARE @ENDDATE DATE='01/MAR/2014'

;WITH  CTE as
(
    SELECT  CAST(@STARTDATE AS DATE) as [DAYS] 
    UNION ALL
    SELECT DATEADD(DAY,1,[DAYS]) [DAYS]
    FROM    CTE
    WHERE   [DAYS] < CAST(@ENDDATE AS DATE)
)
SELECT DISTINCT COUNT([DAYS]) OVER(PARTITION BY DATENAME(WEEKDAY,[DAYS])) CNT,
DATENAME(WEEKDAY,[DAYS]) WD
FROM CTE 
WHERE DATENAME(WEEKDAY,[DAYS]) = 'SATURDAY' OR DATENAME(WEEKDAY,[DAYS]) = 'SUNDAY'
ORDER BY DATENAME(WEEKDAY,[DAYS]) 

Voici votre résultat