Thanks to Motta's help I managed to get into a satisfactory query but I ran into another query because I need to list only the smallest record by cod
, and count the amount of the menorregistro
column by name. >
Could someone help?
Here is the return of the query for example:
Thatis,IneedyoutolistonlytherowswiththeMinorRecordcolumn=558,896,and1321.AndthatitcountstheMinorRegistrobynamegettinglikethis:
Note: I do not need the DT_ATE column and even if I list the smallest record I just want to count how many smaller records each name had, I left the columns just to better illustrate the problem.
Follow the query:
select b.pront as cod,
d.nome,
b.dt_ate,
min(case when b.dt_ate between '19.12.2015' and '08.01.2016' then b.reg else null end) menorRegistro
from recadate b
inner join tbcbopro c on b.crm=c.cod
inner join tbprofis d on c.id_tbprofis=d.id
where b.conv between :ConvInicial and :ConvFinal
and b.unidade between :Unidade_inicial and :Unidade_final
and b.pront in (111811, 210313, 395415)
group by d.nome, b.pront, b.reg, b.dt_ate
having min(case when b.dt_ate between '19.12.2015' and '08.01.2016' then b.reg else null end) is not null