Remove repetitions in sequence from select Oracle

0

I am having a question for mounting a query on Oracle, I have the following so far ...

SELECT M5.NIVEL_ESTRUTURA
     , M5.GRUPO_ESTRUTURA
     , M5.SUBGRU_ESTRUTURA
     , M5.ITEM_ESTRUTURA
     , M5.SEQ_OPERACAO
     , M5.CODIGO_ESTAGIO
  FROM MQOP_050 M5
 WHERE M5.NIVEL_ESTRUTURA = 2
   AND M5.GRUPO_ESTRUTURA = '00504'
   AND M5.NUMERO_ROTEIRO = 1
 ORDER BY M5.SEQ_OPERACAO

Return me ...

AsIjustneedtheCODIGO_ESTAGIO,I'minit...

SELECTM5.CODIGO_ESTAGIOFROMMQOP_050M5WHEREM5.NIVEL_ESTRUTURA=2ANDM5.GRUPO_ESTRUTURA='00504'ANDM5.NUMERO_ROTEIRO=1ORDERBYM5.SEQ_OPERACAO

Returns...

Direct DISTINCT would not solve me because I need to keep the stages in order according to SEQ_OPERACAO, even repeating the codes if they are not in sequence. As an example, what I need is ...

|CODIGO_ESTAGIO
|21
|24
|23
|24
|25

Would anyone help me in this case?

    
asked by anonymous 27.11.2017 / 12:24

2 answers

0

Finally, as I could not do via query, I played this part for application as a method in the class.

It was like this ...

public function getEstagiosPorRoteiro($roteiro = 1) {
    $estagios = array();
    $query = " SELECT M5.CODIGO_ESTAGIO
                 FROM MQOP_050 M5
                WHERE M5.NIVEL_ESTRUTURA = 2
                  AND M5.GRUPO_ESTRUTURA = '$this->grupo'
                  AND M5.NUMERO_ROTEIRO = $roteiro
                ORDER BY M5.SEQ_OPERACAO ";
    $res = db_query($query, 'array');
    if($res) {
        $estagioTemp = 0;
        foreach ($res as $estagio) {
            if($estagioTemp != $estagio) {
                array_push($estagios, $estagio);
                $estagioTemp = $estagio;
            }
        }
        return $estagios;
    }
    return FALSE;
}

Returning an array of stages ...

array
  0 => 
    array
      0 => string '21' (length=2)
      'CODIGO_ESTAGIO' => string '21' (length=2)
  1 => 
    array
      0 => string '24' (length=2)
      'CODIGO_ESTAGIO' => string '24' (length=2)
  2 => 
    array
      0 => string '23' (length=2)
      'CODIGO_ESTAGIO' => string '23' (length=2)
  3 => 
    array
      0 => string '24' (length=2)
      'CODIGO_ESTAGIO' => string '24' (length=2)
  4 => 
    array
      0 => string '25' (length=2)
      'CODIGO_ESTAGIO' => string '25' (length=2)
    
28.11.2017 / 10:59
0
SELECT DISTINCT M5.CODIGO_ESTAGIO
  FROM MQOP_050 M5
 WHERE M5.NIVEL_ESTRUTURA = 2
   AND M5.GRUPO_ESTRUTURA = '00504'
   AND M5.NUMERO_ROTEIRO = 1
 ORDER BY M5.CODIGO_ESTAGIO

To do this use DISTINCT and ORDER BY in the same field In case M5.CODIGO_ESTAGIO

    
28.11.2017 / 11:23