Subquery in MySQL View with GROUP -


i'm trying build view using code:

select *  `ergebnis` left join (     select `nr` `messwert_nr`, `ergebnisnummer`,`teststepreihenfolge`, `messbezeichnung`, `step_bezeichnung`, `messwert`, `messergebnis`     (         select *         `messwerte`         order `nr` desc         ) `f_messwerte`     group `ergebnisnummer`,`teststepreihenfolge`     order `ergebnisnummer`,`teststepreihenfolge` ) `t_messwerte` on (`t_messwerte`.`ergebnisnummer` = `ergebnis`.`nr`) order `nr` desc,`teststepreihenfolge`; 

as can see, relies heavily on order instruction in messwerte on grouped by. subqueries not allowed in views.

assuming source table:

nr  ergebnisnummer  teststepreihenfolge messwert 1   1               1                   80 2   1               1                   86 3   1               2                   306 4   1               2                   302 5   1               2                   304 6   1               3                   0.2 7   2               1                   81 8   2               1                   79 9   2               1                   80 10  2               2                   305 11  2               2                   301 12  2               3                   0.1 13  2               3                   0.3 

this want:

nr  ergebnisnummer  teststepreihenfolge messwert 13  2               3                   0.3 11  2               2                   301 9   2               1                   80 6   1               3                   0.2 5   1               2                   304 2   1               1                   86 

i tried using multiple views, order alway lost, , didn't entry highest nr.

could give me hint on how solve this?

thanks in advance!

 drop table if exists my_table;   create table my_table  (nr  int not null auto_increment primary key  ,ergebnisnummer  int not null  ,teststepreihenfolge int not null  ,messwert decimal(5,2) not null  );   insert my_table values  (1   ,1               ,1                   ,80),  (2   ,1               ,1                   ,86),  (3   ,1               ,2                   ,306),  (4   ,1               ,2                   ,302),  (5   ,1               ,2                   ,304),  (6   ,1               ,3                   ,0.2),  (7   ,2               ,1                   ,81),  (8   ,2               ,1                   ,79),  (9   ,2               ,1                   ,80),  (10  ,2               ,2                   ,305),  (11  ,2               ,2                   ,301),  (12  ,2               ,3                   ,0.1),  (13  ,2               ,3                   ,0.3);   select x.*     my_table x     join        ( select ergebnisnummer              , teststepreihenfolge              , max(nr) max_nr            my_table           group              ergebnisnummer              , teststepreihenfolge       ) y       on y.ergebnisnummer = x.ergebnisnummer      , y.teststepreihenfolge = x.teststepreihenfolge     , y.max_nr = x.nr;   +----+----------------+---------------------+----------+  | nr | ergebnisnummer | teststepreihenfolge | messwert |  +----+----------------+---------------------+----------+  |  2 |              1 |                   1 |    86.00 |  |  5 |              1 |                   2 |   304.00 |  |  6 |              1 |                   3 |     0.20 |  |  9 |              2 |                   1 |    80.00 |  | 11 |              2 |                   2 |   301.00 |  | 13 |              2 |                   3 |     0.30 |  +----+----------------+---------------------+----------+ 

or slower, no subquery...

 select x.*    my_table x    left     join my_table y      on y.ergebnisnummer = x.ergebnisnummer     , y.teststepreihenfolge = x.teststepreihenfolge     , y.nr > x.nr   y.nr null;   +----+----------------+---------------------+----------+   | nr | ergebnisnummer | teststepreihenfolge | messwert |   +----+----------------+---------------------+----------+   |  2 |              1 |                   1 |    86.00 |   |  5 |              1 |                   2 |   304.00 |   |  6 |              1 |                   3 |     0.20 |   |  9 |              2 |                   1 |    80.00 |   | 11 |              2 |                   2 |   301.00 |   | 13 |              2 |                   3 |     0.30 |   +----+----------------+---------------------+----------+ 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -