Search code examples
mysqlsqldatabasetemporal

mysql temporal year


I'm having trouble with the modeling and implementing of "temporal" events in my championship db.

I have three entity: player, team, transfer, championship and I want to track the players' transfer during a championship ( a player can be transferred to another team during a championship)

championship(id_championship*, name, year*)
player (id_player*, name, number, id_team*)
team (id_team, name)
transfer (id_player*, id_team_from*, id_team_to*, transfer _date)

thanks.


Solution

  • You can try this query for example:

    Select T1.id_player, T1.id_team_from as Team, T1.Transfer_date as PeriodFrom, 
    T2.Transfer_date as PeriodTo
                from  
            (Select id_player,id_team_from, id_team_to, Transfer_date, 
            row_number() over (partition by id_player order by transfer_date desc) RN1)T1
                inner join 
            (Select id_player,id_team_from, id_team_to, Transfer_date, 
            row_number() over (partition by id_player order by transfer_date desc) RN2)T2
                on T1.RN1=T2.RN2+1
                and T1.id_player=T2.id_player
    

    It should return all players with teams they played in, from PeriodFrom to PeriodTo.