{"id":3490,"date":"2021-09-01T21:27:17","date_gmt":"2021-09-01T19:27:17","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3490"},"modified":"2021-09-01T21:27:17","modified_gmt":"2021-09-01T19:27:17","slug":"recursive-queries-in-teradata","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3490","title":{"rendered":"Recursive queries in Teradata"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Similarly to Oracle connect by level (see also <a href=\"http:\/\/van-maanen.com\/index.php\/2016\/04\/08\/an-oracle-programme-with-a-loop\/\">http:\/\/van-maanen.com\/index.php\/2016\/04\/08\/an-oracle-programme-with-a-loop\/<\/a> ), Teradata has a structure whereby tables can be connect in order to create a potentially large table. Let us show some code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with\nRECURSIVE jaren(belastingjaar) as\n(\n   select belastingjaar\n   from (select 2015 as belastingjaar)A\n   union all\n   select belastingjaar+1\n   from jaren\n   where belastingjaar &lt; 2020\n)\nselect belastingjaar from jaren;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\"><br>This generates a series going from 2015 to 2020.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\">Or, in another example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with\nRECURSIVE trap(employee_id, naam) as\n(\n    select employee_id , naam\n    from tom.organisatie\n    where employee_id =1\n    union\n    all\n    select organisatie.employee_id, organisatie.naam\n    from tom.organisatie\n    inner join trap\n    on trap.employee_id = organisatie.manager_id\n)\nselect employee_id,naam\nfrom trap;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\">, which is based on this table<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2021\/09\/image-2.png\" alt=\"\" class=\"wp-image-3497\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\">The purpose of the query is to provide an overview of all members who have have employee_id = 1 as their direct or indirect manager. From the table, we see that employee_id = 2,3 have employee_id=1 as their direct manager. However number 4,5 have number 2 as their direct manager and they are therefore indirectly managed by number 1.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\">The query provides the answer. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-5d1ad689-526b-4dfe-bc35-bf005e020298\"><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2021\/09\/image-1-1.png\" alt=\"\" class=\"wp-image-3498\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Similarly to Oracle connect by level (see also http:\/\/van-maanen.com\/index.php\/2016\/04\/08\/an-oracle-programme-with-a-loop\/ ), Teradata has a structure whereby tables can be connect in order to create a potentially large table. Let us show some code: with RECURSIVE jaren(belastingjaar) as ( select belastingjaar from (select 2015 as belastingjaar)A union all select belastingjaar+1 from jaren where belastingjaar &lt; 2020 ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3490","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3490"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3490\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3490"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}