Query SQL, Always show record of a repeating field first

Asked

Viewed 1,026 times

0

I’m having difficulty listing only one line of each process.
The query must always bring the protocols with the last revision.
Ex: Show last revision 2 number process only
and Show only last revision revision process 1

Select 
Numero_Processo Processo
, Numero_Revisao Revisao
,Titulo
,Id
,Data_Criacao
 FROM Qualidade_Insp_Inj_Processo 
 WHERE Data_Exclusao is null

order by Numero_Processo desc, Numero_Revisao desc

I’ve tried Group By, but as the field review is always different it brings all protocols with all revisions

inserir a descrição da imagem aqui

  • There are several ways> to use row_number aggregating by the column you want or a self-join or even a subqueri p/ return the max by some criteria and then return only the records that satisfy the sub-query. Please post a question about query taggear with DBMS as several features are not SQL Ansi

3 answers

1

Try to use it like this:

SELECT
   Numero_Processo Processo
   , Numero_Revisao Revisao
   , Titulo
   , Id
   , Data_Criacao
FROM 
   Qualidade_Insp_Inj_Processo
WHERE
   Id = ( 
      SELECT 
         aux.id 
      FROM
         Qualidade_Insp_Inj_Processo aux 
      WHERE
         aux.Numero_Revisao DESC LIMIT 1 
   ) 
   AND Data_Exclusao IS NULL
ORDER BY 
   Numero_Processo DESC
   , Numero_Revisao DESC
  • It didn’t work, it keeps listing all the processes because the Numero_revisao is different.

1

You can filter through Id with a sub-select by the revision number:

SELECT 
    p.processo,
    p.revisao,
    p.titulo,
    p.id,
    p.data_criacao
FROM 
    processos p
WHERE 
   p.id = (
      SELECT top 1 x.id 
      FROM processos x 
      WHERE x.processo = p.processo 
      ORDER BY revisao DESC
   );

I put in the Sqlfiddle

0


I got it using select MAX inside the Where

SELECT Numero_Processo, Numero_Revisao Revisao,Titulo ,Id ,Data_Criacao 
                                    FROM Qualidade_Insp_Inj_Processo Qproc
                                    WHERE Data_Exclusao is null
                                    AND   Numero_Revisao = (select Max(Numero_Revisao) processo
                                                                from Qualidade_Insp_Inj_Processo
                                                                where Numero_Processo = Qproc.Numero_Processo)

Browser other questions tagged

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