Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Flattening, correlated subquery

445613Jun 13 2005 — edited Jun 14 2005
Hi,

The following fields are key values on a table, with example data :

EMPNO DATE ACTION
1234 6/6/05 HIR
1234 6/6/05 XFR
1234 6/10/05 DTA
1234 6/22/05 XFR
1234 7/30/05 TER
3456 6/6/05 HIR

etc.

I need to create a view to show the previous 5 rows of history per employee but flattened to be one line per Employee Number (EMPNO).

So the view would be something like :

EMPNO ACTION1 DATE1 ACTION2 DATE2 ACTION3 DATE3 (etc.)
1234 HIR 6/6/05 XFR 6/6/05 DTA 6/10/05

I have thought of aliasing the table and then using correlated subqueries, but this got complicated joining to >2 tables. Since joining to table 2 required that DATE1 <> DATE2 or ACTION1 <> ACTION2. But then alias 3 becomes more convoluted and then 4 and 5 OTT.

I also looked at some rownum trickery to get the <=5 rows, but I could not get this working.

What is the best solution for this problem ? Note that it does have to be a pure view, not PL/SQL etc.

Thanks,

T0rrent
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2005
Added on Jun 13 2005
5 comments
452 views