Im really new to SQL. I searched for an answer that would match my requirements AND I would understand what has been done- I failed obv. So here it goes: I am making a programm that would keep data for marathon tournament. So I have a tableStageResults
with columns: StageNo
ParticipantNumber
ParticipantGroup
Time
(as in distance time in full sec's thus int) andPoints
An example would look like:
- 1|01|M21|500|X
- 1|22|M21|550|X
- 1|45|M21|530|X
- 1|47|F09|600|X
- 1|09|F09|630|X
- 2|01|M21|515|X
- 2|45|M21|520|X
So I want the fastest member of each group in each stage to get 1000 points. In the back of my head I feel, that I could just write 1 single query for this, I tried for several hours. Best that I have right now is this:
SELECT c1.ParticipantNumber, c1.ParticipantGroup, c1.Time
FROM StageResults AS c1
LEFT JOIN StageResults AS c2
ON c1.StageNo = c2.StageNo
AND c1.ParticipantGroup = c2.ParticipantGroup
AND c1.Time < c2.Time;
I used this under INSERT statement. No syntax errors but error, that I am trying to insert a duplicate primary key. I think this can be solved by adding GROUP BY statement. So I havent really tested this.
I would ultimately like to set 1000 points for fastest participant in each run(stage) and each group(I mean it should happen automatically). And then based on the fastest guy, calculate points for all other guys.(But thats later and if i figure out how to add these 1k pts, I think ill manage) So I have to add this logic inside UPDATE statement. I am not able to. Im just lost.