If you want to remove the lowest value, this is easy:
select name, (t1 + . . . + t20) - least(t1, . . . , t20) from table;
Unfortunately, MySQL does not have an nth
function, so getting the second smallest and others is quite difficult.
If you have values ββon separate lines, you can do:
select name, sum(t) from (select en.*, if(@name = name, @rn := @rn + 1, @rn := 1) as rn, @name := name from extended_norm en cross join (select @name := '', @rn := 0) const order by name, t desc ) en where rn <= 15 group by name;
With your data structure, you probably need to write a user-defined function to do what you want.
EDIT:
If you need a list t, you can do this in two ways. You can modify the above to include a pivot point (this assumes you have a column called as tnumber
to determine what t value is):
select name, sum(case when rn <= 15 then t end) as Total, max(case when en.tnumber = 1 then t end) as T1, max(case when en.tnumber = 2 then t end) as T2, . . . max(case when en.tnumber = 1 then t end) as T20 from (select en.*, if(@name = name, @rn := @rn + 1, @rn := 1) as rn, @name := name from extended_norm en cross join (select @name := '', @rn := 0) const order by name, t desc ) en group by name;
Otherwise, execute the above query and attach it to the denormalized table:
select e.*, tt.total from extended e join (the above query) tt on e.name = tt.name;