Oracle - Select distinct in one column only

Asked

Viewed 1,128 times

0

I have a table:

aluno1 - data 1 student 1 - date2 student 2 - date1 student 2 - date2

And I want my query to return:

aluno1 - date1 (date + recent) aluno2 - date2 (date + recent)

How do I give a distinct only in the student with the latest date? I tried according to that reply, but I’m not getting the syntax right

   select *
   from (
        select ac.cod_matricula,
          he.cod_usuario_log,
          he.dt_atualiza_log,
          he.txt_ip_log,
          he.cod_usuario_log_del,
          he.dt_atualiza_log_del,
          RANK() OVER(PARTITION BY ac.cod_matricula) rnk    
        from OW.HIST_HISTORICO HE, OW.ALUNO AC
        where ac.cod_aluno_curso = he.cod_aluno_curso
        and he.cod_usuario_log = '1234567'
        --and extract(YEAR from he.dt_atualiza_log) = 2018
        order by he.dt_atualiza_log desc )
   where rnk = 1
  • From what I understand you want to always return the latest date of the dt_actualiz_log column correct? If this is the case, there is no need for Partition by, I could solve using a Max grouping on the date and adding the other fields in the group by.

3 answers

0

As @Confuse said in the comment, you can simply use the grouping with the Max(), in that way:

    select
      ac.cod_matricula,
      he.cod_usuario_log,
      max(he.dt_atualiza_log) dt_atualiza_log,
      he.txt_ip_log,
      he.cod_usuario_log_del,
      he.dt_atualiza_log_del
    from
      OW.HIST_HISTORICO HE, OW.ALUNO AC
    where
      ac.cod_aluno_curso = he.cod_aluno_curso
      and he.cod_usuario_log = '1234567'
    group by
      ac.cod_matricula,
      he.cod_usuario_log,
      he.txt_ip_log,
      he.cod_usuario_log_del,
      he.dt_atualiza_log_del
  • this way he takes the larger dates, but not just selects a license plate

  • 1

    I can’t see how your data is arranged. You can’t make a Sqlfiddle or post an example of some data in table in your question?

0

Well, if you are using Oracle 12c, there is a function that has been incorporated that can help you in your situation, specifically the "FETCH FIRST n ROWS" function, in this article it is possible to see in more detail its operation and explanation of use.

Using the function for your problem, it would be something like:

select ac.cod_matricula,
       he.cod_usuario_log,
       he.dt_atualiza_log,
       he.txt_ip_log,
       he.cod_usuario_log_del,
       he.dt_atualiza_log_del
  from OW.hist_historico he, OW.aluno ac
 where ac.cod_aluno_curso = he.cod_aluno_curso
   and he.cod_usuario_log = '1234567'
 order by he.dt_atualiza_log desc fetch first 1 rows only

In case you first sort by the date you want and then ask to display n lines, in your case it would be 1.

0

Following the instructions of this answer, I believe it is possible to resolve this way:

SELECT rs.* FROM

  (SELECT
    ac.cod_matricula,
    he.cod_usuario_log,
    he.dt_atualiza_log,
    he.txt_ip_log,
    he.cod_usuario_log_del,
    he.dt_atualiza_log_del
  FROM
    OW.HIST_HISTORICO HE, OW.ALUNO AC
  WHERE
    ac.cod_aluno_curso = he.cod_aluno_curso
    and he.cod_usuario_log = '1234567') as rs

WHERE rs.dt_atualiza_log = (SELECT 
                              MAX(he.dt_atualiza_log)
                            FROM
                              OW.HIST_HISTORICO HE, OW.ALUNO AC
                            WHERE
                              ac.cod_aluno_curso = he.cod_aluno_curso
                              AND he.cod_usuario_log = '1234567'
                              AND ac.cod_matricula = rs.cod_matricula)

I didn’t have time to assemble an example dataset to test, in case you have some syntax error I believe you are simple to solve and follow the logic presented in the quoted answer. There are also presented other alternatives, brought only the simplest.

Browser other questions tagged

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