Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mc3ns-0002t6-0k for pgsql-sql@arkaria.postgresql.org; Sun, 17 Oct 2021 10:54:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mc3np-0002yH-RK for pgsql-sql@arkaria.postgresql.org; Sun, 17 Oct 2021 10:54:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mc3np-0002y7-IK for pgsql-sql@lists.postgresql.org; Sun, 17 Oct 2021 10:54:05 +0000 Received: from mout.gmx.net ([212.227.15.19]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mc3nl-0002X7-0L for pgsql-sql@lists.postgresql.org; Sun, 17 Oct 2021 10:54:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1634468038; bh=CxU9U7tZ0WPiFsyk7LnfT3h8+8MdRcRW8Z/xfMp5Yvo=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=CWwKF9T1Osh9iui8D+KZROW6mlhDcQs5aaZJFc1M9ubWnG6m5KEGqocEkMUnE7T1m qkF3rjeoVj1gitVIQguEtZTVJ7NtHxU0Pu9JmUW1IQvxB1Pr4usz47C967n/yLjPdY HeEH7ggrf72grv0z0u7hlbpw9o66H5raHJZaLcEM= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.20] ([83.171.169.85]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MK3Rs-1mHnMO2QGM-00LSU7 for ; Sun, 17 Oct 2021 12:53:58 +0200 Subject: Re: Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle To: pgsql-sql@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <205bcd0b-d74b-b686-ec31-76a04c279a6d@gmx.net> Date: Sun, 17 Oct 2021 12:53:56 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:eUgj6OuvxfUyzaB6FrZMMaGKm89XeaEJLsBFG0gemlmyUFGFNg2 a2p8ABIYzfkPVdeVMEvjSFUjqk7UXdGaD4OWNpNhnzW8q4xTDaJSIA4XvkinFRGIjPyOzNK 2pfJSbdgU8ijO28hPLsBCOAnuQXazm6jo9odlsqo+0jpsBy5KxLnfhV759cS3YGvQri7INI pxZqxbyaoalVf3u1HTXKg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:f2exSc+L9/g=:Dw4POxHxpoh5t1OhxHIDo7 ae2UvQnpcGvwkkNvyi4Z++3IDs02p7gM6DuhwiQ2JwpkbZJg0m8kR69s2IUCBp4CawUMSnBc7 1qorn7hJkw7AzODHvsdmfQSSWZvYpufOEroF8yLTiJo3wqjb60PmMg8QWdUARNLrPiDuVpAEq h9dkiM5SySSUZ3G66KvGuLCVLoQMZ8lXmhYV+hglO6aL3FJu3QpvsdvUj7C7XBqqST4zT5+7f OTPGJNWgbcWKKTj1dFlY5MpRjdjogOqc8mzC/ysR7TiWdo5VJGRQV2c/dG5AjQKTADTdBG2M9 ZzbB4AGxIIFwX6zna7gqlLF69vgDUQXWRlburYLl6DdEKNzCzjAwmcVKrtU1RugUfL3TyPLd5 Eb0RyqBrWpiCrG8rR8yFQ+jzY+AMIbcet24UoZYuHsQDIjcfBpJ49EX4PNQFEwobo1kTq9Uoz D0DaN07j60oCDLU+ErN6ittlg4SPwQ1qXebaIB9E2NyFpiOrioyHaoiANDcfW85HajNYUgEPR RIaNZRWKs7i1V8/pf6usf49Ggj3S0mq15Aj14l3m+H1Svxm9gj02z1tQl8hWj/Yp4TmfKgez4 SDunazEdqoiXHEqbJlUT7w38/rBTn0d2aTDoYGXLxFiGeyerFnkCvd0f8ka3vQ51+3RZj/q0z UIRkMgN/1pIf2VVBTElCf7x6Dxxe9PcUNH64QC54Np8H5nDmdef03LeBw0uytgYsOI4bKTeWw QYus1MsuswbXHnNEE0/QhbvuH6SyfZYwo90MQE1fRTlR8vdjhswv6jY0ErQy7vgXYb3q9Ojas hEl6ngaciObd+ger0ZMEj3C93iol8O8/SPg/usri6UjPPc79pq38Sn1OZI6tthnGMJ/9UA8MY 0VH+ALAtK/anI098qb9QLQN6d0scsHA5qMuJt4M898A6ocGZsa7Fpc4TJC43i2xb4hRjAOO0p 0BoMTLzPtKGfwYjCJTxHcVTyzkCgJdtOBkQhgtuxYYRmInpzL+8C/m1lgzvpe4Yqu2g5qCdPz CSuVy241qfQE55Z/3UrXTFkm/cVYPvUyWgCLzjEZf6rUNDhQaCE0fN0s2HWZ+mKcZ4FI1GEsI zea+dRGK210f2E= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jian He schrieb am 17.10.2021 um 10:36: > https://stackoverflow.com/questions/22626394/does-postgresql-have-a-pseu= do-column-like-level-in-oracle > Wandering around, playing around, then problems come. I tried to > crack the *level *concept . So I followed through with the most voted > answer from the above link. The following is my code sample data. > > begin; > create temptable tempemp (employee_idinteger primary key, last_name tex= t,manager_idinteger); > insert into tempempvalues(1,'eliane',1); > insert into tempempvalues(2,'sponge',1); > insert into tempempvalues(3,'george',1); > insert into tempempvalues(4,'kramer',2); > insert into tempempvalues(5,'megan',2); > insert into tempempvalues(6,'donald',3); > commit; > > WITH RECURSIVE cteAS ( > SELECT employee_id, last_name, manager_id,1 AS level > FROM tempemp > > UNION ALL > SELECT e.employee_id, e.last_name, e.manager_id, c.level +1 > FROM cte c > JOIN tempemp eON e.manager_id =3D c.employee_id > ) > SELECT * > FROM cte; > This row: > insert into tempempvalues(1,'eliane',1); creates an endless loop because it points to itself. If there is no manager assigned you should use NULL instead Additionally your recursive CTE does not have a condition for the starting= element If you want to stick with the circular reference of an employee to itself,= you need to exlcude the starting element WITH RECURSIVE cte AS ( SELECT employee_id, last_name, manager_id, 1 AS level FROM tempemp where employee_id =3D 1 UNION ALL SELECT e.employee_id, e.last_name, e.manager_id, c.level + 1 FROM tempemp e JOIN cte c ON e.manager_id =3D c.employee_id where e.employee_id <> 1 ) SELECT * FROM cte; But it would be better to use: insert into tempempvalues(1,'eliane', null); Then you don't need to exclude the root element in the recursive part: WITH RECURSIVE cte AS ( SELECT employee_id, last_name, manager_id, 1 AS level FROM tempemp where manager_id is null UNION ALL SELECT e.employee_id, e.last_name, e.manager_id, c.level + 1 FROM tempemp e JOIN cte c ON e.manager_id =3D c.employee_id ) SELECT * FROM cte;