Remove repetitions in select Oracle sequence

Asked

Viewed 120 times

0

I have a question to mount 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

Call me back...

inserir a descrição da imagem aqui

Since I only need CODIGO_ESTAGIO, I’m in it...

SELECT 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

Returns...

inserir a descrição da imagem aqui

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

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

Someone would know to assist me in this case?

  • Can’t do what you want through SELECT.

  • Yeah, I guess I’ll have to do it at the application level anyway... Thanks @Reginaldorigo

2 answers

0


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

Stayed 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;
}

Thus 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)

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 the DISTINCT AND ORDER BY in the same field In case the M5.CODIGO_ESTAGIO

  • Hello @Marcos, actually this is not quite what I need, as I described above the distinct does not solve because stage 24 must repeat, and I can not lose the ordering.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.