Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Select max problem
Hello,
"Marc-Antoine" <marco_at_systemmedia.com> wrote in message
news:200439-121951-517067_at_foorum.com...
>
> Hello,
>
> I have this table :
>
> id1 id2 date_begin date_end id
> 1 1 10/03/2004 16/03/2004 3
> 2 1 10/03/2004 17/03/2004 3
> 3 2 10/03/2004 17/03/2004 4
> 4 2 11/03/2004 16/03/2004 4
>
> I would like to get id1 for each id and date_begin = max(date_begin)
> *but* if there are many rows returned (id = 3 in this sample), I must get
also
> max(date_end)
> (in this sample, I should get id = 3, id1 = 2 and id = 4, id1 = 4 )
>
> I've tried with group by having without success...
>
> Thanks in advance
It's relatively easy with analytics:
select * from (
select id,
id1, date_begin, date_end, max_begin, max_end, count(*) over (partition by id) cnt from (select id, id1, date_begin, date_end, max(date_begin) over (partition by id) max_begin, max(date_end) over (partition by id) max_end from t1)
VC Received on Tue Mar 09 2004 - 09:45:27 CST