Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f70Fq-0003U7-RN for pgsql-general@arkaria.postgresql.org; Fri, 13 Apr 2018 15:04:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f70Fo-0004Y0-Ut for pgsql-general@arkaria.postgresql.org; Fri, 13 Apr 2018 15:04:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f70Fo-0004Xr-EX for pgsql-general@lists.postgresql.org; Fri, 13 Apr 2018 15:04:44 +0000 Received: from mail-pl0-x22f.google.com ([2607:f8b0:400e:c01::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f70Fl-0001Ls-82 for pgsql-general@lists.postgresql.org; Fri, 13 Apr 2018 15:04:42 +0000 Received: by mail-pl0-x22f.google.com with SMTP id 91-v6so6248869pld.3 for ; Fri, 13 Apr 2018 08:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=zhsNdJFRsqxt0+c9QeLiVKPRmp/OHTRkseuNBBjkx2E=; b=y6xVsX6l6Ay5H7u82jgNhxrgN4LWa0pLhde9IoVLCnKHdED7RMwN9HGPfeyzpBl05o XVkeeyQaeGZ6bf/mNIe9GydEsSZN9HsatPsM30+WQyqKTdLBVgX9+gd7sUNwY16hrb8I es3AZs3TDwAWsxL9MgMrVfD7+8kflnqOhVgRWpSsR7Y3P4O2WyZ3qDjIVIZmFMplqOmg GmHiQ0PdAMApSTAXaCJh68PsbafS53dfSEaOIFHmsGC3CVrN0yIV2sCozZrj5INsXn1B MFURLc3Rw9JluFa9oKefqYD7+4NNYhwtgEvX+b8aLmfyJD6k1Tc0vjsz5CqDuMvAvWN4 dbyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=zhsNdJFRsqxt0+c9QeLiVKPRmp/OHTRkseuNBBjkx2E=; b=kJwi7EiST61lJNEMPTeQTpYpTmnL7Mw9FNDytC0hwl7MyYo5nAi7UpXzkdo+XbuK+v K7O+fNogTlgT21SZK+e3emuvzIovUvLFOqg6e5AiAhPI/8UprlIjJNsw2rnschelfNWn tamFir4ZcPW7eSAmVuQkg6BlWeCdNK/8cA16BJfD7cxSE/hFlD5vpbJrgQsFJJU6qyk4 PSZ8J/XqjhBzi3PZBuemO8vmZgiJTk9my4hO3R/9eDgggkLkbunrbEKHvBzUN0txdVfZ RRHZDEpD9FBnwdUI0L4tfBacIF4Dq+0OuN0ykxAACIYCYn2aGIg6KZpmxV6tmrjpmW9X yQBQ== X-Gm-Message-State: ALQs6tBdLoxKYtPWd/E0c4r8wl0oAlRUr+EOqRUu8/1BiY6NpMntD4ZE eMsyY8j4W36i9qb/uij/BkDQhodU X-Google-Smtp-Source: AIpwx4+peCMevj48DJT4R0+z5n0cYAVO5uhDbzriia5mJ/qImXqa47kUORSWVYePXBxiMUu4xftd+A== X-Received: by 2002:a17:902:a70c:: with SMTP id w12-v6mr5470470plq.74.1523631879095; Fri, 13 Apr 2018 08:04:39 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id u7sm16356167pgc.71.2018.04.13.08.04.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Apr 2018 08:04:38 -0700 (PDT) Subject: Re: Recursive CTE for building menus To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: Date: Fri, 13 Apr 2018 08:04:37 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/13/2018 02:09 AM, Bob Jones wrote: > The adaptions I am trying to make are as follows: > - Higher priority moves the item higher up the menu (i.e. adapting > from the original "votes" concept). > - Default alphabetical ordering of titles > - Use of alphanumeric IDs instead of numeric Hi, I wrote that blog post! :-) This works for me: WITH RECURSIVE cte (menu_item_id, menu_title, path, menu_parent, depth, menu_priority) AS ( SELECT menu_item_id, menu_title, ARRAY[(-menu_priority, menu_title, menu_item_id)] AS path, menu_parent, 1 AS depth, menu_priority FROM test_table WHERE menu_parent IS NULL UNION ALL SELECT m.menu_item_id, m.menu_title, cte.path || (-m.menu_priority, m.menu_title, m.menu_item_id), m.menu_parent, cte.depth + 1, m.menu_priority FROM test_table m JOIN cte ON m.menu_parent = cte.menu_item_id ) SELECT menu_item_id, menu_title, path, depth, menu_priority FROM cte ORDER BY path ; menu_item_id | menu_title | path | depth | menu_priority --------------+--------------------+----------------------------------------------------------------------------------------------+-------+--------------- H | Home | {"(-1000,Home,H)"} | 1 | 1000 A | About | {"(-900,About,A)"} | 1 | 900 B | Background | {"(-900,About,A)","(,Background,B)"} | 2 | NULL R | Resources | {"(-900,About,A)","(,Resources,R)"} | 2 | NULL F | FOOBAR | {"(-800,FOOBAR,F)"} | 1 | 800 Fb | Bar | {"(-800,FOOBAR,F)","(,Bar,Fb)"} | 2 | NULL Fba | About Bar | {"(-800,FOOBAR,F)","(,Bar,Fb)","(,\"About Bar\",Fba)"} | 3 | NULL Fbt | Team Bar | {"(-800,FOOBAR,F)","(,Bar,Fb)","(,\"Team Bar\",Fbt)"} | 3 | NULL Ff | Foo | {"(-800,FOOBAR,F)","(,Foo,Ff)"} | 2 | NULL Ffw | Foo World | {"(-800,FOOBAR,F)","(,Foo,Ff)","(,\"Foo World\",Ffw)"} | 3 | NULL FFwi | World Introduction | {"(-800,FOOBAR,F)","(,Foo,Ff)","(,\"Foo World\",Ffw)","(-1000,\"World Introduction\",FFwi)"} | 4 | 1000 FFwa | About World | {"(-800,FOOBAR,F)","(,Foo,Ff)","(,\"Foo World\",Ffw)","(,\"About World\",FFwa)"} | 4 | NULL (12 rows) So basically the sort is by menu_priority, breaking ties with menu_title, then breaking ties with menu_item_id. I think that's what you want, right? The hard part was dealing with mixed types (integer for priority, text for the others), because an array has to be all one type. Fortunately you can build an array of tuples and the sorting will work as you expect. I was a little worried to see those tuples appearing like strings in the output, but then I remembered that in Postgres ' is a string and " is not. Or to prove it: select * from unnest( array[(1, 'a'::text), (2, 'b'::text)] ) x(a int, b text); a | b ---+--- 1 | a 2 | b Anyway, I hope that gets you what you need! Yours, -- Paul ~{:-) pj@illuminatedcomputing.com