tags:

views:

213

answers:

1

I read this article, and i have the code in oracle, but I want to convert it to work on MySQL. In Oracle, i use the function rank, with four columns can eligible or not, how i can use this in mysql, or, it is not possible?

This is the code, I want select the most eligible line, every line can have 4 columns completed, I want to rank one of which has more data.

SELECT vlr,
       data
INTO   vn_vlr,
       vd_data
FROM (SELECT a.*, rank() over (ORDER BY nvl(a.id_categoria, -1) DESC,
             nvl(a.id_peso, -1) DESC,
             nvl(a.id_faixa, -1) DESC,
             nvl(a.sexo, ' ') DESC ) rank
        FROM tab_regra_pagamento a
       WHERE a.id_competicao = pidcompedticao
         AND a.tipo = 'NORMAL'
         AND a.data_vencimento > SYSDATE
         AND nvl(a.id_categoria, vid_categoria) = vid_categoria
         AND nvl(a.id_faixa, vid_faixa) = vid_faixa
         AND nvl(a.id_peso, vid_peso) = vid_peso
         AND nvl(a.sexo, vsexo) = vsexo)
WHERE  rank = 1;
A: 
SELECT  @rank := @rank + (@value <> value),
        @value := value
FROM    (
        SELECT  @rank := 0,
                @value := -1
        ) vars,
        mytable
ORDER BY
        value

In you case, when you just need all copies of the first set of values:

SELECT  vlr, data
FROM    tab_regra_pagamento a
WHERE   a.id_competicao = pidcompedticao
        AND a.tipo = 'NORMAL'
        AND a.data_vencimento > SYSDATE
        AND (a.id_cetegoria, a.id_faixa, a.id_peso, a.sexo) =
        (
        SELECT  ai.id_cetegoria, ai.id_faixa, ai.id_peso, ai.sexo
        FROM    tab_regra_pagamento ai
        WHERE   ai.id_competicao = pidcompedticao
                AND ai.tipo = 'NORMAL'
                AND ai.data_vencimento > SYSDATE
        ORDER BY
                a.id_cetegoria DESC, a.id_faixa DESC, a.id_peso DESC, a.sexo DESC
        LIMIT 1
        )
Quassnoi