Hi,
I have a table like the below with scores.
CREATE TABLE [dbo].[Student_Scores](
[Month] [int] NULL,
[Student_no] [bigint] NULL,
[Score] [numeric](18, 2) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES (202507, 87, CAST(0.00 AS Numeric(18, 2)))
INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES (202505, 87, CAST(97.60 AS Numeric(18, 2)))
INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES (202506, 87, NULL)
INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES (202508, 87, CAST(89.20 AS Numeric(18, 2)))
INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES (202509, 87, CAST(0.00 AS Numeric(18, 2)))
GO
For any new month where the score is a null or a 0, that month should take the score of the last period that had a score.
So for example,
- the final score for the student for 202505 will be 97.6.
- the final score for 250506 will be 97.6 (because 202506 is null, we take the latest score that is not null or 0)
- the final score for 250507 will be 97.6 (because 202507 is a 0, we take the latest score that is not null or 0)
- the final score for 250508 will be 89.2 (because 202508, we take the score that is not null or 0
- the final score for 250509 will be 89.2 (because 202509 is a 0, we take the latest score that is not null or 0)
- and so on...
Kindly advise
Thanks