I'm using SQL Server, I have a table with 3 columns (timeseries) data
, with date, hour
beginning, AwardStatus
.
The award status for the most part is randomly generated. There can be two options, Awarded or Not Awarded.
However, the business requirement is that we MUST print 'NotAwarded' for 3 consecutive rows if the status is NotAwarded, and 4 consecutive rows if the status is Awarded.
Goal: a new column ShouldBe
details.
Once it meets the minimum requirements, then it checks that the current row's AwardStatus
and continues to overwrite the logic.
Question: Is that possible in SQL without any kind of cursor/looping?
The picture in below as an example.
Here's an example:
AwardStatusMinimum 3
AwardStatusMaximum 4
Date Hour AwardStatus ShouldBe
--------------------------------------
1/1/2019 1 NotAwarded NotAwarded
1/1/2019 2 NotAwarded NotAwarded
1/1/2019 3 Awarded NotAwarded
1/1/2019 4 Awarded Awarded
1/1/2019 5 NotAwarded Awarded
1/1/2019 6 NotAwarded Awarded
1/1/2019 7 Awarded Awarded
1/1/2019 8 NotAwarded NotAwarded
1/1/2019 9 Awarded NotAwarded
1/1/2019 10 Awarded NotAwarded