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  |
|  ....  |  .... | ..  | ..  | ..  | ..  | .. | ..  | ..  |
+--------+-------+-----+-----+-----+-----+----+-----+-----+

TOTALで注文。この合計は、ベスト オブ 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機能がないため、2 番目に小さいものを取得するのは非常に困難です。

別々の行に値がある場合は、次のことができます。

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 のリストが必要な場合は、2 つの方法があります。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 に答える