Hello everyone,
I need your guidance if possible, the output of my sql statement produces 12 months of data and stops.
I am not sure how to increment the @ReleaseYear by +1 to continue with the loop and retrieve the next 12 months of data, until I reach the @FinalReleaseYear = 2003
Please help if you can or point me in the right direct
Thank you for reading.
DECLARE @ReleaseMonth int
DECLARE @ReleaseYear int
DECLARE @FinalReleaseYear int
SET @ReleaseMonth = 1
SET @ReleaseYear = 2000
SET @FinalReleaseYear = 2003
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
I need your guidance if possible, the output of my sql statement produces 12 months of data and stops.
I am not sure how to increment the @ReleaseYear by +1 to continue with the loop and retrieve the next 12 months of data, until I reach the @FinalReleaseYear = 2003
Please help if you can or point me in the right direct
Thank you for reading.
DECLARE @ReleaseMonth int
DECLARE @ReleaseYear int
DECLARE @FinalReleaseYear int
SET @ReleaseMonth = 1
SET @ReleaseYear = 2000
SET @FinalReleaseYear = 2003
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