Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Doing a top N after self join
Hi, I have a table with an enddate, and I want to join each row with the row
one day ago (i.e. yesterday with respect to the current row), then I want to
return the top N days.
I'd like to do it like:
select *
from (
/* join today and yesterday */ select today.*, yesterday.* from ( /* assign a daynum after sorting data */ select rownum daynum, inner.* from ( /* sort all the data */ select * from mytable order by enddate ) inner
) today,
( select rownum daynum, inner2.* from ( select * from mytable order by enddate ) inner2
) yesterday
where yesterday.daynum = today.daynum - 1 order by today.daynum desc )
My problem is after I do the join and sort desc, my columns are ambiously defined. I'd perfer not to have to alias every column from each table.
Is there a way to access the today.* and yesterday.* columns?
thanks
Jake
Received on Wed Aug 06 2003 - 10:41:57 CDT