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

  • Home
  • SEARCH
  • 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 8025091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:03:22+00:00 2026-06-04T23:03:22+00:00

I’ve read through the Oracle documentation concerning the CONNECT operations, but I can’t seem

  • 0

I’ve read through the Oracle documentation concerning the CONNECT operations, but I can’t seem to get my head around a database query we have in an existing application. Below is a simplified version of the query.

SELECT LEVEL,
       CONNECT_BY_ROOT MY_MONTH MY_LABEL,
       b.*
FROM (
    SELECT ROWNUM AS ORDERING, 
           MY_AREA,
           TRUNC (THE_MONTH, 'MONTH') AS MY_MONTH
    FROM MY_TABLE
    ORDER BY MY_AREA, MY_MONTH DESC
) b
WHERE LEVEL <= 3
START WITH 1 = 1
CONNECT BY PRIOR MY_AREA = MY_AREA
       AND PRIOR ORDERING = ORDERING - 1
       AND PRIOR MY_MONTH <= ADD_MONTHS(MY_MONTH, 6);

While I have a basic understanding of the CONNECT functionalities, this combination has me lost. Can anyone explain what is going on in this query?

I think the end says to get all of the rows that have the same area and a row number 1 less than the current row number and a date before 6 months in the future from the current date. I would guess this would only return 1 row (due to the row number criteria) or 0 rows if the other criteria weren’t met. And then maybe the first CONNECT_BY_ROOT says to get that row’s MY_MONTH value?

  • 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-06-04T23:03:24+00:00Added an answer on June 4, 2026 at 11:03 pm

    Start with b, which is a table of MY_AREA (a number?), MY_MONTH, which is a month-truncated date (i.e. the days are all set to 01), and an aliased ROWNUM, which is determined by the ORDER BY clause, which is ORDER BY MY_AREA, MY_MONTH DESC, e.g.:

    +----------+---------+-----------+
    | ORDERING | MY_AREA | MY_MONTH  |
    +----------+---------+-----------+
    | 1        | 10      | 01-SEP-12 |
    | 2        | 10      | 01-JAN-12 |
    | 3        | 12      | 01-AUG-12 |
    | 4        | 12      | 01-JUN-12 |
    | 5        | 12      | 01-MAY-12 |
    | 6        | 12      | 01-JAN-12 |
    | 7        | 12      | 01-JAN-10 |
    +----------+---------+-----------+
    

    The WHERE clause doesn’t come into play until later, so move on to START WITH, which says only 1 = 1. This means that every row in b will be used in the query; if you had had another condition here, e.g. my_area < 5 or whatever, only a certain set of rows would have been used.

    Now, the CONNECT BY, which determines how the hierarchy should be built. This works like a WHERE clause, except for the special PRIOR keyword which tells the DB to look at the previous level in the hierarchy. So:

    • PRIOR MY_AREA = MY_AREA just means that the child node has to have the same value for `MY_AREA’
    • PRIOR ORDERING = ORDERING - 1 means that the child should come one row after the current node in b‘s ordering.
    • PRIOR MY_MONTH <= ADD_MONTHS(MY_MONTH, 6) means that in order to be joined into the hierarchy, the previous MY_MONTH should be 6 months or less after the date of the current node.

    The whole hierarchy is then created. LEVEL (special for CONNECT BY…) is set to the level in the hierarchy, CONNECT_BY_ROOT gives the MY_MONTH value for the root of that hierarchy and aliases it to MY_LABEL. After this, the table would look something like the following table. I’ve added separators for each hierarchy for clarity.

    +-------+-----------+----------+---------+-----------+
    | LEVEL | MY_LABEL  | ORDERING | MY_AREA | MY_MONTH  |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-SEP-12 | 1        | 10      | 01-SEP-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-JAN-12 | 2        | 10      | 01-JAN-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-AUG-12 | 3        | 12      | 01-AUG-12 |
    | 2     | 01-AUG-12 | 4        | 12      | 01-JUN-12 |
    | 3     | 01-AUG-12 | 5        | 12      | 01-MAY-12 |
    | 4     | 01-AUG-12 | 6        | 12      | 01-JAN-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-JUN-12 | 4        | 12      | 01-JUN-12 |
    | 2     | 01-JUN-12 | 5        | 12      | 01-MAY-12 |
    | 3     | 01-JUN-12 | 6        | 12      | 01-JAN-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-MAY-12 | 5        | 12      | 01-MAY-12 |
    | 2     | 01-MAY-12 | 6        | 12      | 01-JAN-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-JAN-12 | 6        | 12      | 01-JAN-12 |
    +-------+-----------+----------+---------+-----------+
    | 1     | 01-JAN-10 | 7        | 12      | 01-JAN-10 |
    +-------+-----------+----------+---------+-----------+
    

    So, as you can see, each of the rows appears at the top of its own hierarchy, with all nodes meeting the CONNECT BY criteria under it.

    Finally, the WHERE clause is applied; this chops off all of the levels > 3 in every hierarchy, so you’re left with a maximum of 3 levels. This affects only one row in the middle hierarchy, the one with LEVEL = 4.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

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.