mariadb - SQL - sum top 3 values in the row as a total of the row -


i trying sum row - top 3 values in row... possible?

name - race1 - race2 - race3 - race4 - race5 - total    - null  - 10    -   9   -   5   -  4    - 25 b    - 10    - 3     - null  -   7   -  3    - 20 c    - 4     - null  - null  - null  -  2    - 6 ... 

is possible? know how sum values in row...

select name,  (coalesce(race1,0) + coalesce(race2,0) + coalesce(race3,0) + coalesce(race4,0) + coalesce(race5,0)) total view_race_results 1; 

thanks hint. using 10.1.19-mariadb. zdenka

i have original data in table in columns can use it.... have problem parameter... parameter a.model in clause unknown (i have tried exist when trying use parameter main query in subquery.... , lost... idea?

    select a.model, a. name, a.surname, a.agetype, a.license, a.klub, (select b.origpoints view_micr_result_data b b.race='race1' , a.model=b.model) race1, (select b.origpoints view_micr_result_data b b.race='race2' , a.model=b.model) race2, (select b.origpoints view_micr_result_data b b.race='race3' , a.model=b.model) race3, (select b.origpoints view_micr_result_data b b.race='race4' , a.model=b.model) race4, (select b.origpoints view_micr_result_data b b.race='race5' , a.model=b.model) race5, (select sum(x.origpoints) (select origpoints view_micr_result_data model=a.model limit 3) x ) total view_model 1; 

unpivot , use variables before aggregating:

select name, sum(race) (select name, race,              (@rn := if(@n = name, @rn + 1,                         if(@n := name, 1, 1)                         )              ) rn       ((select name, race1 race t) union             (select name, race2 race t) union             (select name, race3 race t) union             (select name, race4 race t) union             (select name, race5 race t)             ) t cross join            (select @n := '', @rn := 0) params       race not null       order name, race desc      ) nr rn <= 3 group name; 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -