I have this little bit of code that I can’t quite work correctly. The part giving me trouble is this:
IF( Class_Subset = @prevSub AND RegNum = @prevNum AND `DRI` >99.99 ,
@Platinum :=@Platinum + 1 ,'' )
What I need to do is tell the @Platinum to reset back to zero once the Class_Subset, or RegNum conditions are no longer met.
Any ideas?
Whole code:
SELECT
Harley.Hgt, Harley.RegNum, Harley.Callname, Harley.OLastname,
Harley.Tpe, Harley.Points, Harley.Class, Harley.Total_Points,
Harley.Title, Harley.Platinum, Harley.Silver_Purple,
Harley.Date_Earned, Harley.Judge, Harley.HostClub, Harley.DRI,
Harley.Class_Subset, Harley.IncorrectRegNum, Harley.MemNum, Harley.ID
FROM
(
SELECT
Hgt, RegNum, Callname, OLastname, Tpe, Points, Class,
Total_Points, Title, Silver_Purple, Date_Earned, Judge,
HostClub, DRI, Class_Subset, IncorrectRegNum, MemNum, ID,
IF (
Class_Subset = @prevSub
AND RegNum = @prevNum
AND `DRI` > 99.99,
@Platinum := @Platinum + 1,
0
) AS Platinum,
@prevSub := Class_Subset,
@prevNum := RegNum
FROM
Harley,
(
SELECT @Platinum := 0, @prevSub := '', @prevNum := ''
) r
ORDER BY RegNum, Class_Subset, Date_Earned
) Harley
This is what the file looks like, you can see why it needs to remember the variable while the RegNum and Subset match, but then reset to 0 once they change:
I think I now see the issue.
The row set where
Platinumis being calculated is ordered byRegNumandClass_Subset(in particular), which is fine as the calculation does rely on those columns being sorted. But theIF()condition also involves theDRIcolumn, and, as can be seen on your screenshot, a sequence ofDRIvalues greater than99.99may be interrupted within a single group of(RegNum, Class_Subset). When it is reinstated, you need the Platinum values to carry on, not to reset to1.Therefore, your
@Platinumexpression should be a bit more sophisticated. Here’s one way of changing it: