4

我有这张桌子:扩展

+--------+-----+-----+-----+-----+-  -+-----+-----+
| Name   |  T1 | T2  | T1  | T3  | .. | T19 | T20 |
+--------+-----+-----+-----+-----+-  -+-----+-----+
| john   |  5  | 10  | 50  | 10  | .. | 20  | 8   |
| bill   |  2  | 8   | 11  | 5   | .. | 9   | 55  |
| james  |  30 | 15  | 12  | 40  | .. | 13  | 10  |
| elsie  |  28 | 35  | 20  | 32  | .. | 18  | 1   |
|  ....  |  .. | ..  | ..  | ..  | .. | ..  | ..  |
+--------+-----+-----+-----+-----+-  -+-----+-----+

我想退回这个:

+--------+-------+-----+-----+-----+-----+-  -+-----+-----+
| Name   | TOTAL |  T1 | T2  | T1  | T3  | .. | T19 | T20 |
+--------+-------+-----+-----+-----+-----+-  -+-----+-----+
| bill   |  250  |  2  | 8   | 11  | 5   | .. | 9   | 55  |
| john   |  230  |  5  | 10  | 50  | 10  | .. | 20  | 8   |
| elsie  |  158  |  28 | 35  | 20  | 32  | .. | 18  | 1   |
| james  |  129  |  30 | 15  | 12  | 40  | .. | 13  | 10  |
|  ....  |  .... | ..  | ..  | ..  | ..  | .. | ..  | ..  |
+--------+-------+-----+-----+-----+-----+----+-----+-----+

按总计排序。这个总数是最好的 15 个 Tx 的总和......

我现在不知道如何做到这一点。

该表来自另一个包含大量数据的表的请求( CREATE VIEW )。

你能帮助我吗 ?

在这一点上,我做了所有 Tx 的总和,但这不是我想要的......

SELECT `Name`, (T1+T2+ T3+T4+T5+T6+T7+T8+T9+T10+T11+T12+T13+T14+T15+T16+T17+T18+T19+T20) AS TOTAL, T1,T2, T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20
FROM `extended`
ORDER BY TOTAL DESC
4

2 回答 2

2

如果您想删除最低值,这很容易:

select name,
       (t1 + . . . + t20) -
        least(t1, . . . , t20)
from table;

不幸的是,MySQL没有这个nth功能,因此获得第二名和其他人是相当困难的。

如果您将值放在单独的行中,则可以执行以下操作:

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;

使用您的数据结构,您可能需要编写一个用户定义的函数来执行您想要的操作。

编辑:

如果你想要 t 的列表,你可以用两种方法。您可以修改上述内容以包括枢轴(这假设您有一个名为类似的列tnumber来识别哪个 t 值):

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;

否则,采用上述查询并将其加入非规范化表:

select e.*, tt.total
from extended e join
     (the above query) tt
     on e.name = tt.name;
于 2013-09-13T11:59:19.810 回答
0

这很尴尬,但这也可以工作!

select name, sum(t) from 
(select name, t from (
     select  name, t1 as t from extended
     union 
     select  name, t2 as t from extended
      union 
     select  name, t3 as t from extended
      union 
     select  name, t4 as t from extended
      union 
     select  name, t5 as t from extended
      union 
     select  name, t6 as t from extended
       union 
     select  name, t7 as t from extended
       union 
     select  name, t8 as t from extended
       union 
     select  name, t9 as t from extended
       union 
     select  name, t10 as t from extended
       union 
     select  name, t11 as t from extended
        union 
     select  name, t12 as t from extended
        union 
     select  name, t13 as t from extended
        union 
     select  name, t14 as t from extended
        union 
     select  name, t15 as t from extended
        union 
     select  name, t16 as t from extended
        union 
     select  name, t17 as t from extended
        union 
     select  name, t18 as t from extended
        union 
     select  name, t19 as t from extended
        union 
     select  name, t20 as t from extended
     ) z
    where name='bill' order by name, t desc limit 0,15);

'bill'但是您必须通过替换为其他名称来为每个用户运行查询

于 2013-09-13T12:56:19.957 回答