Query Question?
Posted in 1997
I am trying to select the most row from a group rows based on the date the
row was inserted. I must get only the the most current row in each case.
Here are some tries that did not work.
long fld1
long fld2
date fld3
date fld4
date fld5
date fld6
long group
select fld1, fld2, fld3, fld4, fld5, fld6, group
from table1 t1
where (select max(fld3)
table1 t2
where t1.fld1 = t2.fld1
and t1.fld2 = t2.fld2)
and (fld4 is not null
or fld5 is not null
or fld6 is not null)
group by t1.fld1, t1.fld2, t1.fld3, fld4, fld5, fld6, group;
I found out using this query that the only effect the subselect has on the
query results are not desired. Every row in the table was selected.
So I read up on group by. Why because the next attempt also resulted in
all rows being selected and I discovered that in order to use group by and
having I need to list all the columns being selected in the group by
clause. However, this seems to result a set of all rows.
If I say
select fld1, fld2, max(fld3) fld4, fld5, fld6
from table1 t1
group by t1.fld1, t1.fld2, t1.fld3, fld4, fld5, fld6;
So how do I select the most current row in a group