Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
|
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2005, 12:12 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Retreive latest and greatest record

Hi,

I have an ACCOMPLISHMENTS table which has a Foreign key to itself:

ID STATUS COPIED_ID ACCOMPLISHMENT_DATE
-- -------------- --------- -------------------
1 Approved 01-JAN-2000
2 Archived 1 01-JAN-2001
3 Pending Review 01-JAN-2005
4 Disapproved 2 01-JAN-2002
5 In Progress 4 01-JAN-2003


So, here, there is a hierarchy of records. For eg., ID 2 is the child of ID 1, ID 4 is the child of ID 2 and ID 5 is the child of ID 4. ID 3 is a standalone record without any child records:

Approved
Approved->Archived
Approved->Archived->Disapproved
Approved->Archived->Disapproved->In Progress
Pending Review

I want to query the latest and greatest always and display to the user thru a JSP. For eg., if I query this table without any parameters (query criteria), I should get:

ID STATUS
-- --------------
3 Pending Review
5 In Progress

This is so, because ID 3 has no child records and ID 5 is the last child in all that hierarchy of records. If I query with a query criteria saying I want to see only those records with status of "Approved", then it should show:

ID STATUS
-- --------
1 Approved

Or if I say I want to see all accomplishments between 01-JAN-1999 to 31-DEC-2002 then I should see only:

ID STATUS
-- -----------
4 Disapproved


Can anyone help?

Thanks
Al
 
Old April 4th, 2005, 12:28 PM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Al,

Does the hierarchy have a maximum depth, or is it able to grow as deep as desired?

Beth

 
Old April 5th, 2005, 07:59 AM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it can grow as deep as required
 
Old April 25th, 2005, 09:57 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!
I think you'd better write function

FUNCTION GET_DETAILS ( intID IN NUMBER) RETURN VARCHAR2
IS
   strReturnValue VARCHAR2(4000);
   intCurrID NUMBER;
   strCurrValue VARCHAR2(50);
BEGIN
    -- Init value
    strReturnValue :='';
    intCurrID := intID;
    -- Do loop
    WHILE NOT intCurrID IS NULL LOOP
        EXIT WHEN intCurrID IS NULL;
        SELECT COPIED_ID, STATUS INTO intCurrID, strCurrValue
        FROM ACCOMPLISHMENTS
        WHERE ID = intCurrID;
        IF LENGTH(strReturnValue) > 0 THEN
            strReturnValue := strCurrValue || '->' || strReturnValue;
        ELSE
            strReturnValue := strCurrValue;
        END IF;
    END LOOP;
    RETURN strReturnValue;
END;

Now! You write SQL

SELECT GET_DETAILS(ID)
from ACCOMPLISHMENTS

Good Luck!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting distinct and latest record elygp SQL Language 4 May 4th, 2008 10:49 PM
get latest record frm transaction tbl jyopallavi SQL Server 2000 1 June 9th, 2005 08:11 AM
How to retreive record ... arshad mahmood SQL Server 2000 1 August 16th, 2003 01:38 AM
How to retreive record ... arshad mahmood MySQL 2 August 14th, 2003 02:18 AM
Retreive Record arshad mahmood VB Databases Basics 2 July 23rd, 2003 08:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.