$30 off During Our Annual Pro Sale. View Details »

10 Features do Oracle que voce não conhecia - CONNECT BY CLAUSE

10 Features do Oracle que voce não conhecia - CONNECT BY CLAUSE

(Palestra ministrada no TechDay da MDIAS em JAN-2019)

Todo banco de dados possui features na qual desconhecemos, o que acaba nos levando a soluções mais complicadas ou menos eficientes, com o Oracle não é diferente. Por esse motivo, pretendo apresentar de forma bastante didática uma série com 10 features do Oracle 11g que eu desconhecia e que tem facilitado bastante minha vida desde meados de 2016.

Como primeira feature, apresento o CONNECT BY CLAUSE, na qual nos permite escrever consultas hierárquicas de forma simples levando todo o processamento para dentro do banco de dados. Além disso, também apresento outras maneiras de tirar proveito desta feature no dia a dia.

Rafael Ponte

January 31, 2019
Tweet

More Decks by Rafael Ponte

Other Decks in Technology

Transcript

  1. 10 features do Oracle
    que você não conhecia
    #1 CONNECT BY CLAUSE
    TechDay 2019 - @rponte

    View Slide

  2. describe tb_funcionarios;

    View Slide

  3. Name Null? Type
    ---------- -------- -------------
    ID NOT NULL NUMBER(38)
    NOME NOT NULL VARCHAR2(200)
    CARGO NOT NULL VARCHAR2(100)
    GERENTE_ID NUMBER(38)
    describe tb_funcionarios;

    View Slide

  4. Name Null? Type
    ---------- -------- -------------
    ID NOT NULL NUMBER(38)
    NOME NOT NULL VARCHAR2(200)
    CARGO NOT NULL VARCHAR2(100)
    GERENTE_ID NUMBER(38)
    describe tb_funcionarios;

    View Slide

  5. Name Null? Type
    ---------- -------- -------------
    ID NOT NULL NUMBER(38)
    NOME NOT NULL VARCHAR2(200)
    CARGO NOT NULL VARCHAR2(100)
    GERENTE_ID NUMBER(38)
    describe tb_funcionarios;
    (FOREIGN KEY)

    View Slide

  6. Name Null? Type
    ---------- -------- -------------
    ID NOT NULL NUMBER(38)
    NOME NOT NULL VARCHAR2(200)
    CARGO NOT NULL VARCHAR2(100)
    GERENTE_ID NUMBER(38)
    describe tb_funcionarios;
    (FOREIGN KEY)

    View Slide

  7. select f.*
    from tb_funcionarios f
    ;

    View Slide

  8. select f.*
    from tb_funcionarios f
    ;

    View Slide

  9. select f.*
    from tb_funcionarios f
    ;

    View Slide

  10. select f.*
    from tb_funcionarios f
    ;

    View Slide

  11. select f.*
    from tb_funcionarios f
    ;

    View Slide

  12. select f.*
    from tb_funcionarios f
    ;

    View Slide

  13. select f.*
    from tb_funcionarios f
    ;

    View Slide

  14. select f.*
    from tb_funcionarios f
    ;

    View Slide

  15. select f.*
    from tb_funcionarios f
    ;

    View Slide

  16. select f.*
    from tb_funcionarios f
    ;

    View Slide

  17. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    Hierarquia

    View Slide

  18. Sabe aquele relatório?

    View Slide

  19. select f.nome as funcionario
    from tb_funcionarios f
    order by f.nome
    ;

    View Slide

  20. select f.nome as funcionario
    from tb_funcionarios f
    order by f.nome
    ;

    View Slide

  21. E o gerente?

    View Slide

  22. select f1.nome as funcionario
    ,f2.nome as gerente
    from tb_funcionarios f1
    ,tb_funcionarios f2
    where f1.gerente_id = f2.id
    order by f.nome
    ;

    View Slide

  23. select f1.nome as funcionario
    ,f2.nome as gerente
    from tb_funcionarios f1
    ,tb_funcionarios f2
    where f1.gerente_id = f2.id
    order by f.nome
    ;

    View Slide

  24. E o gerente do
    gerente?

    View Slide

  25. select f1.nome as funcionario
    ,f2.nome as gerente
    ,f3.nome as gerente_do_gerente
    from tb_funcionarios f1
    ,tb_funcionarios f2
    ,tb_funcionarios f3
    where f1.gerente_id = f2.id
    and f2.gerente_id = f3.id
    order by f.nome
    ;

    View Slide

  26. select f1.nome as funcionario
    ,f2.nome as gerente
    ,f3.nome as gerente_do_gerente
    from tb_funcionarios f1
    ,tb_funcionarios f2
    ,tb_funcionarios f3
    where f1.gerente_id = f2.id
    and f2.gerente_id = f3.id
    order by f.nome
    ;

    View Slide

  27. E o gerente do gerente
    do gerente … do
    gerente?

    View Slide

  28. Qual a profundidade
    dessa hierarquia?

    View Slide

  29. Ou faz na aplicação,
    né?

    View Slide

  30. View Slide

  31. CONNECT BY CLAUSE

    View Slide

  32. CONNECT BY CLAUSE
    Oracle

    View Slide

  33. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  34. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  35. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  36. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  37. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  38. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  39. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  40. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  41. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  42. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio

    View Slide

  43. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  44. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  45. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  46. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  47. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  48. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  49. View Slide

  50. PRIOR
    (acessando informações do parent-node)

    View Slide

  51. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,????? as gerente_nome
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  52. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,????? as gerente_nome
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  53. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,prior f.nome as gerente_nome
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  54. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,prior f.nome as gerente_nome
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  55. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,prior f.nome as gerente_nome
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  56. Mas tem algo
    estranho…

    View Slide

  57. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;

    View Slide

  58. Stevens?

    View Slide

  59. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    View Slide

  60. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    View Slide

  61. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    View Slide

  62. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    Root Node

    View Slide

  63. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    ;
    Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    Root Node

    View Slide

  64. Mas como chego na
    raiz?

    View Slide

  65. select f.*
    from tb_funcionarios f
    ;

    View Slide

  66. select f.*
    from tb_funcionarios f
    ;

    View Slide

  67. select f.*
    from tb_funcionarios f
    ;

    View Slide

  68. select f.*
    from tb_funcionarios f
    ;

    View Slide

  69. Então vamos começar
    a hierarquia por ele…

    View Slide

  70. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  71. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  72. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  73. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  74. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  75. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  76. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  77. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  78. E se houver muitas
    raizes?

    View Slide

  79. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id = 3
    ;

    View Slide

  80. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id in (3, 45, 104)
    ;

    View Slide

  81. Mas o ID vem de uma
    sequence, e agora?

    View Slide

  82. select f.*
    from tb_funcionarios f
    ;

    View Slide

  83. select f.*
    from tb_funcionarios f
    ;

    View Slide

  84. select f.*
    from tb_funcionarios f
    ;

    View Slide

  85. select f.*
    from tb_funcionarios f
    ;

    View Slide

  86. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.id in (3, 45, 104)
    ;

    View Slide

  87. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  88. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  89. Como visualizar melhor essa
    hierarquia?

    View Slide

  90. Profundidade

    View Slide

  91. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    View Slide

  92. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    1

    View Slide

  93. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    1
    2

    View Slide

  94. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    1
    2
    3

    View Slide

  95. Chris
    Rafael
    Plauto
    Ricardo
    Stevens
    Thaio
    Principe
    Guto …
    Victor Aldeir
    Maia
    Pimentel Roberto

    1
    2
    3
    4
    5
    6

    View Slide

  96. Profundidade

    (level)

    View Slide

  97. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  98. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  99. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  100. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  101. Stevens
    Thaio
    Principe Victor Aldeir

    View Slide

  102. Stevens
    Thaio
    Principe Victor Aldeir
    .Stevens
    ..Thaio
    ...Principe
    ...Victor
    ...Aldeir

    View Slide

  103. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  104. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  105. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  106. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  107. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  108. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || id as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  109. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  110. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    ;

    View Slide

  111. Ordenando…

    View Slide

  112. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  113. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order by f.nome
    ;

    View Slide

  114. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order by f.nome
    ;

    View Slide

  115. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order by f.nome
    ;
    ????

    View Slide

  116. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order by f.nome
    ;

    View Slide

  117. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order by f.nome
    ;

    View Slide

  118. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  119. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  120. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;
    x

    View Slide

  121. Outra forma de
    apresentar...

    (path)

    View Slide

  122. Outra forma de
    apresentar...

    (path)
    Chris
    Chris->Ricardo
    Chris->Ricardo->Plauto
    Chris->Ricardo->Plauto->Stevens
    Chris->Ricardo->Plauto->Stevens->Thaio
    Chris->Ricardo->Plauto->Stevens->Thaio->Aldeir
    Chris->Ricardo->Plauto->Stevens->Thaio->Principe
    Chris->Ricardo->Plauto->Stevens->Thaio->Victor
    Chris->Ricardo->Plauto->Stevens->Thaio->…
    Chris->Ricardo->Plauto->Guto

    View Slide

  123. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  124. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  125. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  126. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  127. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  128. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  129. Loop infinito?

    View Slide

  130. UPDATE tb_funcionarios
    SET gerente_id = 1 -- Plauto
    WHERE id = 3 -- Chris
    ;

    View Slide

  131. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  132. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  133. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  134. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;
    ERROR:
    ORA-01436: CONNECT BY loop in user data

    View Slide

  135. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  136. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  137. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  138. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_cycle as is_cycle
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  139. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  140. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_cycle as is_cycle
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  141. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_cycle as is_cycle
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id = 3
    order siblings by f.nome
    ;

    View Slide

  142. Tem mais?

    View Slide

  143. UPDATE tb_funcionarios
    SET gerente_id = null
    WHERE id = 3 -- Chris
    ;

    View Slide

  144. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle

    ,connect_by_isleaf as is_leaf
    ,connect_by_root f.id as root_id
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  145. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle

    ,connect_by_isleaf as is_leaf
    ,connect_by_root f.id as root_id
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  146. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle

    ,connect_by_isleaf as is_leaf
    ,connect_by_root f.id as root_id
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  147. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle

    ,connect_by_isleaf as is_leaf
    ,connect_by_root f.id as root_id
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  148. select f.id as id
    ,f.nome as funcionario
    ,f.cargo as cargo
    ,f.gerente_id as gerente_id
    ,level
    ,lpad('.', (level-1), '.') || f.nome as tree
    ,sys_connect_by_path(f.nome, '->') as path
    ,connect_by_iscycle as is_cycle

    ,connect_by_isleaf as is_leaf
    ,connect_by_root f.id as root_id
    from tb_funcionarios f
    connect by nocycle f.gerente_id = prior f.id
    start with f.gerente_id is null
    order siblings by f.nome
    ;

    View Slide

  149. Dá pra fazer mais?

    View Slide

  150. SELECT level
    FROM DUAL
    CONNECT BY level <= 100
    ;

    View Slide

  151. SELECT level
    FROM DUAL
    CONNECT BY level <= 100
    ;

    View Slide

  152. SELECT level
    FROM DUAL
    CONNECT BY level <= 100
    ;

    View Slide

  153. INSERT INTO t(seq)
    SELECT level
    FROM DUAL
    CONNECT BY level <= 100
    ;

    View Slide

  154. INSERT INTO tb(seq)
    SELECT level
    FROM DUAL
    CONNECT BY level <= 100
    ;

    View Slide

  155. SELECT
    add_months(input, (level-1) * 12) as start_date
    ,add_months(input, level * 12) as end_date
    FROM (
    -- Set the input date here
    SELECT DATE'2012-02-01' as input
    FROM DUAL
    )
    CONNECT BY
    add_months(input, (level-1) * 12) < Sysdate
    ;

    View Slide

  156. SELECT
    add_months(input, (level-1) * 12) as start_date
    ,add_months(input, level * 12) as end_date
    FROM (
    -- Set the input date here
    SELECT DATE'2012-02-01' as input
    FROM DUAL
    )
    CONNECT BY
    add_months(input, (level-1) * 12) < Sysdate
    ;

    View Slide

  157. SELECT
    add_months(input, (level-1) * 12) as start_date
    ,add_months(input, level * 12) as end_date
    FROM (
    -- Set the input date here
    SELECT DATE'2012-02-01' as input
    FROM DUAL
    )
    CONNECT BY
    add_months(input, (level-1) * 12) < Sysdate
    ;

    View Slide

  158. SELECT
    add_months(input, (level-1) * 12) as start_date
    ,add_months(input, level * 12) as end_date
    FROM (
    -- Set the input date here
    SELECT DATE'2012-02-01' as input
    FROM DUAL
    )
    CONNECT BY
    add_months(input, (level-1) * 12) < Sysdate
    ;

    View Slide

  159. SELECT
    add_months(input, (level-1) * 12) as start_date
    ,add_months(input, level * 12) as end_date
    FROM (
    -- Set the input date here
    SELECT DATE'2012-02-01' as input
    FROM DUAL
    )
    CONNECT BY
    add_months(input, (level-1) * 12) < Sysdate
    ;
    x
    está incluindo hoje,
    2019-01-31

    View Slide

  160. Concluindo

    View Slide

  161. Obrigado!
    TechDay 2019 - @rponte

    View Slide