sql >> Database >  >> RDS >> Sqlserver

Hoe krijg ik het aantal weekenddagen van een reeks datums

Hier is het

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]) 

Hier is je resultaat



  1. hoe bereik te berekenen in orakel

  2. Rails Postgresql meerdere schema's en dezelfde tabelnaam

  3. PASS Summit 2013 :Een succes in Charlotte

  4. Laden van klasse com.mysql.jdbc.Driver ... is verouderd bericht