Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:34+00:00 2026-05-28T23:34:34+00:00

I believe the Oracle function FIRST_VALUE is what I need to be using based

  • 0

I believe the Oracle function FIRST_VALUE is what I need to be using based on these two questions:
SQL – How to select a row having a column with max value
Oracle: Taking the record with the max date

I have 3 tables that represent people associated with organizations. Each organization may have a parent org, where ORG.PARENT is a foreign key to ORG.ID (so the table refers to itself). A person may be associated with more than one group.

PERSON

ID    NAME
----------
1     Bob

ORG

ID    NAME        PARENT
------------------------
1     A           (null)
2     A-1              1
3     A-2              1
4     A-3              1
5     A-1-a            2
6     A-1-b            2
7     A-2-a            3
8     A-2-b            3

PERSON_TO_ORG

PERSON_ID  ORG_ID
-----------------
    1        1
    1        3

I want to list the groups a person is associated with so I used this query:

SELECT NAME, ID, sys_connect_by_path(NAME, '/') AS path
FROM org
START WITH ID IN
(SELECT org_id FROM person_to_org WHERE person_id=1)
connect by prior org.ID = org.parent;

…which gives me:

NAME    ID    PATH
------------------
A-2     3     /A-2
A-2-a   8     /A-2/A-2-a
A-2-b   9     /A-2/A-2-b
A       1     /A
A-1     2     /A/A-1
A-1-a   5     /A/A-1/A-1-a
A-1-b   6     /A/A-1/A-1-b
A-2     3     /A/A-2
A-2-a   8     /A/A-2/A-2-a
A-2-b   9     /A/A-2/A-2-b
A-3     4     /A/A-3

Notice how A-2 appears twice, as it should. I don’t want a group to appear twice, however. I want a group to only appear at its lowest level in the tree, i.e. at its highest level value. Here is how I’ve tried using FIRST_VALUE with no luck – I still get A-2 (and others) appearing twice:

SELECT id, name, path, first_value(lev) OVER
(
PARTITION BY ID,NAME, path ORDER BY lev DESC
) AS max_lev FROM
(SELECT NAME, ID, sys_connect_by_path(NAME, '/') AS path, LEVEL as lev
FROM org START WITH ID IN
(SELECT org_id FROM person_to_org WHERE person_id=1)
connect by prior org.ID = org.parent);

This seems similar to the FIRST_VALUE example in Pro Oracle SQL but I can’t seem to make it work no matter how I tweak the parameters.

How can I return only the rows where a given group has its highest level value (i.e. farthest down in the tree)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:34:35+00:00Added an answer on May 28, 2026 at 11:34 pm

    As also said in one of the threads you refer to, analytics are not the most efficient way to go here: you need to aggregate to filter out the duplicates.

    SQL> SELECT id
      2       , max(name) keep (dense_rank last order by lev) name
      3       , max(path) keep (dense_rank last order by lev) path
      4    FROM ( SELECT NAME
      5                , ID
      6                , sys_connect_by_path(NAME, '/') AS path
      7                , LEVEL as lev
      8             FROM org
      9            START WITH ID IN (SELECT org_id FROM person_to_org WHERE person_id=1)
     10          connect by prior org.ID = org.parent
     11         )
     12   group by id
     13  /
    
            ID NAME  PATH
    ---------- ----- --------------------
             1 A     /A
             2 A-1   /A/A-1
             3 A-2   /A/A-2
             4 A-3   /A/A-3
             5 A-1-a /A/A-1/A-1-a
             6 A-1-b /A/A-1/A-1-b
             7 A-2-a /A/A-2/A-2-a
             8 A-2-b /A/A-2/A-2-b
    
    8 rows selected.
    

    Regards,
    Rob.

    PS: Here is some more information about the LAST aggregate function: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions071.htm#sthref1495

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Oracle server (version 9, I believe) and a MS SQL server
I have a few questions about using mysql and oracle in a PHP app.
I'm using a pdf package for oracle pl/sql called pl_fpdf to create pdfs on
I believe I need a DTD to define the schema and an XSLT if
I believe strongly in using unit-tests as part of building large multi-platform applications. We
Converting from usind Micorsofts Syste.Data.OracleClient to what I believe is called Oracles ODT (Oracle.DataAccess
I am using Oracle 10g and the following paradigm to get a page of
I need to copy some data from one table to another in Oracle, while
While researching Oracle Analytics, I came across this query: select an_id, a_date, min(a_date) over
I need to diagram an oracle database and I am hoping to find some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.