The statement below returns data for the 12 months of year 2000. I am not sure how to increment the year by 1 and loop through again to insert data for year 2001.
Any thoughts or examples would be appreciated, thank you for reading.
DECLARE @ReleaseMonth int
DECLARE @ReleaseYear varchar(10)
SET @ReleaseMonth = 1
SET @ReleaseYear = '2000'
WHILE @ReleaseMonth < 13
BEGIN
--INSERT INTO ALOS
SELECT @ReleaseMonth AS'MonthNumber'
,DATENAME(mm,DateAdd(mm,@ReleaseMonth,-1))AS Month
,COUNT(AR.Offender_Number)Released
,SUM(LengthOfStay) AS [TotalDays]
,SUM(CONVERT(DECIMAL(10,2),LengthOfStay))/Count(AR.Offender_Number)AS [Average Length of Stay]
,@ReleaseYear AS 'Year'
FROM Admission_Release AR INNER JOIN
Offenders O ON AR.Offender_Number = O.Offender_Number
WHERE ReleaseMonth= DATENAME(mm,DateAdd(mm,@ReleaseMonth,-1)) AND ReleaseYear=@ReleaseYear
AND AR.Release_Age >= 18
SET @ReleaseMonth = @ReleaseMonth +1
END
Any thoughts or examples would be appreciated, thank you for reading.
DECLARE @ReleaseMonth int
DECLARE @ReleaseYear varchar(10)
SET @ReleaseMonth = 1
SET @ReleaseYear = '2000'
WHILE @ReleaseMonth < 13
BEGIN
--INSERT INTO ALOS
SELECT @ReleaseMonth AS'MonthNumber'
,DATENAME(mm,DateAdd(mm,@ReleaseMonth,-1))AS Month
,COUNT(AR.Offender_Number)Released
,SUM(LengthOfStay) AS [TotalDays]
,SUM(CONVERT(DECIMAL(10,2),LengthOfStay))/Count(AR.Offender_Number)AS [Average Length of Stay]
,@ReleaseYear AS 'Year'
FROM Admission_Release AR INNER JOIN
Offenders O ON AR.Offender_Number = O.Offender_Number
WHERE ReleaseMonth= DATENAME(mm,DateAdd(mm,@ReleaseMonth,-1)) AND ReleaseYear=@ReleaseYear
AND AR.Release_Age >= 18
SET @ReleaseMonth = @ReleaseMonth +1
END