How does a Recursive CTE run, line by line?

后端 未结 5 1981
情书的邮戳
情书的邮戳 2020-11-27 12:33

I think I\'ve got the format of Recursive CTEs down well enough to write one, but still find myself frustrated to no end that I cannot manually process one (pretend to be th

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 13:10

    Step 1:

    1 Europe NULL Europe
    2 Asia   NULL Asia
    

    Step 2:

    1 Europe  NULL Europe
    2 Asia    NULL Asia
    3 Germany 1    Europe/Germany
    4 UK      1    Europe/UK
    5 China   2    Asia/China
    6 India   2    Asia/India
    

    Step 3:

    1 Europe   NULL Europe
    2 Asia     NULL Asia
    3 Germany  1    Europe/Germany
    4 UK       1    Europe/UK
    5 China    2    Asia/China
    6 India    2    Asia/India
    7 Scotland 4    Europe/UK/Scotland
    

    Step 4:

    1 Europe    NULL Europe
    2 Asia      NULL Asia
    3 Germany   1    Europe/Germany
    4 UK        1    Europe/UK
    5 China     2    Asia/China
    6 India     2    Asia/India
    7 Scotland  4    Europe/UK/Scotland
    8 Edinburgh 7    Europe/UK/Scotland/Edinburgh
    

    Step 5:

    1 Europe    NULL Europe                             0
    2 Asia      NULL Asia                               0
    3 Germany   1    Europe/Germany                     1
    4 UK        1    Europe/UK                          1
    5 China     2    Asia/China                         1
    6 India     2    Asia/India                         1
    7 Scotland  4    Europe/UK/Scotland                 2
    8 Edinburgh 7    Europe/UK/Scotland/Edinburgh       3
    9 Leith     8    Europe/UK/Scotland/Edinburgh/Leith 4
    

    The last column in step 5 is the Level. During each level the rows get added with respect to what is already available. Hope this helps.

提交回复
热议问题