SQL - Query Doubt

Asked

Viewed 32 times

2

Guys I have a tremendous doubt, I have these 3 columns need only bring the smallest record if the date is between the between. Example: Case put date 23/12/2015 a 01/01/2016 It is not to bring record because the smallest record has date 18/12/2015.

Tabela

Follow my query but it is wrong, because it is bringing the smallest record even if the date is not 18/12/2015.

select min(b.reg) Menor_Reg, b.dt_ate, b.pront
from recadate b
inner join tbcbopro c on b.crm=c.cod
inner join tbprofis d on c.id_tbprofis=d.id
inner join tbcbosus e on e.id=c.id_tbcbosus
inner join repacagd f on f.crm=b.crm
inner join tbconven g on b.conv=g.cod
where b.dt_ate between '23.12.2015' and '01.01.2016'
and b.pront=116312
group by b.pront, dt_ate

Resultado da query

I wanted to bring the smallest record only if the date was for example 18/12/2015.

Thanks since, already, any help is welcome...

1 answer

2


But the date you want 18/12/2015 is not between the range 23.12.2015 and 01.01.2016, seems to bring the smallest only if the lowest general is in the requested range , want to do when not bring null ?

Try something like this yet

select min(b.reg) Menor_geral, 
       min(case when b.dt_ate between '23.12.2015' and '01.01.2016' then b.reg else null end) menor_intervalo,
b.dt_ate, b.pront
from recadate b
inner join tbcbopro c on b.crm=c.cod
inner join tbprofis d on c.id_tbprofis=d.id
inner join tbcbosus e on e.id=c.id_tbcbosus
inner join repacagd f on f.crm=b.crm
inner join tbconven g on b.conv=g.cod
where b.pront=116312
group by b.pront, dt_ate
  • Dear congratulations, it worked perfectly did not imagine that a simple case could solve this.. Thanks really !

  • Motta as it would be if I wanted to bring only lines where the short_range is <> null?

  • having min(case when b.dt_ate between '23.12.2015' and '01.01.2016' then b.reg Else null end) is null

Browser other questions tagged

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