I see a solution that uses the windows row_number () functions and an extra column in the database to order at each level (or recursive CTE in SQL Server). However, SQLite does not support this.
Here is my recommendation for implementing the solution without making many requests back:
(1) Assign tee order for the first tee.
(2) For each next tee, pay attention to the previous score and previous order:
(3) Assign a new triple order by going over the previous scores, ordering the highest DESC score and the previous ASC tiy order.
Since you only have a few players per round, it makes sense to do this at the application level. However, if you have a database that supports the window function, then you can easily make a solution only for the database.
I can not resist. Here is the code that will do this with a table for storing orders. You need to scroll once per hole:
create table ThisOrder ( ThisOrderId int primary key autoincrement, RoundId int, Hole int, PlayerId int )
Initialize it with each player in a certain order.
Then insert new rows into the table for each hole:
insert into ThisOrder(RoundId, HoleId, PlayerId) select s.RoundId, s.Hole+1, s.PlayerId from Scores s join ThisOrder to on s.PlayerId = to.PlayerId and s.RoundId = to.RoundId and s.Hole = to.Hole order by s.Score DESC, to.Order ASC
You will need to call it once for each hole, minus one.
Then receive your order as:
select * from ThisOrder where roundid = <roundid> and hole = <thehole> order by ThisOrderId