1

I have this query that has a '+level' with the date. I am not sure what it is but executing the query gives the list of dates in an ascending order. Here's how the query looks like:

select date '2018-01-01' + level -1 dt
from dual connect by level <= 1000

Any guidance?

Super.Sam
  • 37
  • 1
  • 5

2 Answers2

1

CONNECT BY specifies the relationship between parent rows and child rows of the hierarchy.

More information here - Hierarchical Queries

So the date 01-JAN-18 would be at root (level 1) and then the dates forward (restricted to 1000 levels) would be children.

Level 1 would be a parent for Level 2 and so on.

select date '2018-01-01' + level -1 dt, LEVEL
from dual connect by level <= 1000

For more, refer to this example from here.

SELECT employee_id, last_name, manager_id FROM employees CONNECT BY PRIOR employee_id = manager_id;

EMPLOYEE_ID LAST_NAME                 MANAGER_ID
    101     Kochhar                          100
    108     Greenberg                        101
    109     Faviet                           108
    110     Chen                             108
    111     Sciarra                          108
    112     Urman                            108
    113     Popp                             108
    200     Whalen                           101

Note here, the manager with id 100 is at the top of the organization.

Shreyas
  • 999
  • 6
  • 19
0

While Shreyas is correct, a more simplified answer for your example is that "level" is like a variable that comes along when you use connect by and is like a counter for the connect by "loop". It will cause the query to be run 1000 times and level will hold the index as it loops. Here it's used in the calculation and will increment from 1 to 1000.

Gary_W
  • 9,933
  • 1
  • 22
  • 40