date - MYSQL - SUM values of last 10 days -
i have table in mysql:
| player1 | player2 | date | fs_1 | fs_2 | jack tom 2015-03-02 10 2 mark riddley 2015-05-02 3 1 ...
i need know how many aces (fs_1) player 1 have done before match reported in date_g (10 days before example).
this tried without success:
option 1
select players_atp.name_p 'player 1', p.name_p 'player 2', date(date_g) 'date', result_g 'result', fs_1, fs_2, sum(if(date_sub(date_g, interval 10 day)< date_g, fs_1, 0)) 'last 10 days' stat_atp stat_atp join backup3.players_atp on id1 = id_p join backup3.players_atp p on p.id_p = id2 join backup3.games_atp on id1_g = id1 , id2_g = id2 , id_t_g = id_t , id_r_g = id_r date_g > '2015-01-01' group id1;
option 2
select players_atp.name_p 'player 1', p.name_p 'player 2', date(date_g) 'date', result_g 'result', fs_1, fs_2, sum(case when date_g between date_g , date_sub(date_g, interval 10 day) fs_1 else 0 end) 'last 10 days' stat_atp stat_atp join backup3.players_atp on id1 = id_p join backup3.players_atp p on p.id_p = id2 join backup3.games_atp on id1_g = id1 , id2_g = id2 , id_t_g = id_t , id_r_g = id_r date_g > '2015-01-01' group id1;
i have edited code, more easy read , understand.
select id1 'player 1', id2 'player 2', date(date_g) 'date', result_g 'result', fs_1, fs_2, sum(case when date_g between date_g , date_sub(date_g, interval 10 day) fs_1 end) 'last 20 days' stat_atp stat_atp join backup3.games_atp on id1_g = id1 , id2_g = id2 , id_t_g = id_t , id_r_g = id_r group id1;
thanx in advance.
maybe you:
select id1, sum(fs_1) stat_atp date_g <= date_sub('2015-03-02', interval 1 day) , date_g >= date_sub('2015-03-02', interval 10 day) , id1='jack' group id1;
remember rdbms used construct rigorous data sets linked between each others clear ids (keys when talking sql). it's easier respect 3 first normal forms. that's why should use keys identify match itself. way use subqueries (subsets) achieve goal.
then, keep in mind sql structured. it's force , weakness cause won't able use turing complete programming langage loops , conditions. in situation able find same structure query. can interact sql query result langage , use loops , condition on result set itself. that's you.
anyway, may want read mysql group clause different iso sql form : https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
Comments
Post a Comment