Question

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 table StageResults with columns: StageNo ParticipantNumber ParticipantGroup Time(as in distance time in full sec's thus int) and Points 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.

Any advice is welcome. Maybe im thinking in the wrong direction completely on how to do this. Any help will be much appreciated.

Was it helpful?

Solution

The query that identifies the rows might look like this:

select t.*
from table t
where not exists (select 1
                  from table t2
                  where t2.ParticipantGroup = t.ParticipantGroup and
                        t2.StageNo = t.StageNo and
                        t2.time < t.time
                 );

The question is then how you turn this into an update. For MySQL, you would do:

update table StageResults sr join
             (select t.*
              from table t
              where not exists (select 1
                                from table t2
                                where t2.ParticipantGroup = t.ParticipantGroup and
                                      t2.StageNo = t.StageNo and
                                      t2.time < t.time
                                )
             ) toupdate
             on toupdate.ParticpantNumber = sr.ParticpantNumber
    set sr.points = sr.points + 1000; 

The syntax for SQL Server would be a bit different, but your question is tagged MySQL.

EDIT:

For SQL Server:

with toupdate as (select t.*
                  from table t
                  where not exists (select 1
                                    from table t2
                                    where t2.ParticipantGroup = t.ParticipantGroup and
                                          t2.StageNo = t.StageNo and
                                          t2.time < t.time
                                    )
                 )
update toupdate
    set points = points + 1000;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top