Select the line with the oldest date within this query between the repeated values

Asked

Viewed 1,324 times

0

select e.nm_equipto as EQUIPAMENTOS
    ,Max(remessa.dt_uso_inicio) as DATA_REMESSA --,(SELECT MAX(DT_ENTORC_OFICINA) FROM ORCOS)as DATA_ABERTURA 
    ,MIN(dt_entorc_oficina) as DATA_ABERTURA 
    ,o.numero as NÚMERO 
    ,patr.nr_patrimonio as PATRIMÔNIO 
    ,DATEDIFF ( DAY , MAX(remessa.dt_uso_inicio), o.dt_entorc_oficina)as QTD_DIAS
from orcos as o
inner join equipto as e on o.cd_equipto = e.cd_equipto
inner join patrimon as patr on o.cd_patrimonio = patr.cd_patrimonio
inner join est_almox as almox on almox.cd_almox = patr.cd_almox
inner join v_remessa_patrimonio as remessa on remessa.cd_patrimonio = patr.cd_patrimonio
group by o.numero
    ,patr.nr_patrimonio
    ,o.dt_entorc_oficina
    ,patr.cd_almox
    ,e.nm_equipto
having o.dt_entorc_oficina > max(remessa.dt_uso_inicio)
    and count(o.numero) > 1
    and patr.cd_almox = '6'
order by o.numero desc

![The data are in this table 1]

inserir a descrição da imagem aqui

  • the earliest date in the DATA_ABERTURA column

  • 1

    Please don’t keep posting duplicate questions. That won’t help you to get a faster answer. Instead of flooding the site, try to be clearer in your explanation and add details to the existing question. Duplicate link: http://answall.com/q/192170/18246

  • @Janderhelius: Could you add above (in the description of the problem) additional information to facilitate understanding of the problem as well as help in posting suggestions? For example, what is the purpose of the query, what contains each table involved etc.

1 answer

0


Considering the complete code you posted on this topic, here’s another suggestion:

-- código #2 v5
with tbOPR as (
SELECT r.dt_uso_inicio as DATA_REMESSA,
       o.dt_entorc_oficina as DATA_ABERTURA,
       o.numero as NÚMERO,
       o.cd_equipto,
       p.nr_patrimonio as PATRIMÔNIO, 
       p.cd_almox,
       Seq= row_number() over (partition by p.nr_patrimonio,
                                            r.dt_uso_inicio
                               order by o.dt_entorc_oficina asc)
  from orcos as o
       inner join patrimon as p on o.cd_patrimonio = p.cd_patrimonio
       inner join v_remessa_patrimonio as r on r.cd_patrimonio = p.cd_patrimonio
  where r.dt_uso_inicio < o.dt_entorc_oficina
        and p.cd_almox = '6'
)
SELECT OPR.cd_almox,
       e.nm_equipto as EQUIPAMENTOS,
       OPR.DATA_REMESSA, OPR.DATA_ABERTURA, OPR.NÚMERO, OPR.PATRIMÔNIO,
       QTD_DIAS= DateDiff(day, OPR.DATA_REMESSA, OPR.DATA_ABERTURA)
  from tbOPR as OPR
       inner join equipto as e on OPR.cd_equipto = e.cd_equipto
  where OPR.Seq = 1;
  • @Josédias tested here and even so persists the duplicity and DATA_REMESSA has to be smaller than DATA_ABERTURA and in the case where it is: WHERE P.CD_ALMOX = '6' is to take only some types of equipment and is picking up even those that do not belong to this warehouse. I have tried several ways and remains persistent to duplicity. Thank you very much!

  • @Janderhelius: regarding "DATA_REMESSA has to be smaller than DATA_ABERTURA", I added this restriction in the WHERE clause of CTE tbOPR. // Already about "P.CD_ALMOX = '6' is to take only some types of equipment and is picking up even those that do not belong to this warehouse", for verification I added the column p.cd_almox in the display of the final result. How is the p.cd_almox column declared? // To get feedback from the proposed suggestion, please test code #2 v5 as is, without modifying it.

  • @Janderhelius: if duplicities persist, check in the database definition how the junctions between tables are. Maybe there’s something missing from the FROM clause.

  • managed to do. Thank you so much for the help. Great Hug!

Browser other questions tagged

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