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 1n55GE-0007pH-1T for pgsql-sql@arkaria.postgresql.org; Wed, 05 Jan 2022 12:19:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n55GC-0005Yk-Q0 for pgsql-sql@arkaria.postgresql.org; Wed, 05 Jan 2022 12:19:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n55GC-0005Ya-DH for pgsql-sql@lists.postgresql.org; Wed, 05 Jan 2022 12:19:20 +0000 Received: from mail.klingler.net ([5.189.138.105]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n55G9-000058-Fc for pgsql-sql@lists.postgresql.org; Wed, 05 Jan 2022 12:19:19 +0000 Message-ID: <24fd6621-db02-24a1-0808-fd11a17fe262@klingler.net> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=klingler.net; s=2020; t=1641385154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=T1ko8VIK9Bt59+R1ZyZ+Db0yJJKKNgNfIej/mqWJiGs=; b=ss1PJ5y7oDdhV3on3FarS1qmFvG/2RGrccORgjJVdsCuCIyzTkcYLDEXjy1yV+OT8XzoL4 FnogEXrObpVja/oGuzyazncCuULXqOttBy8gAwjCxzrPs1Dz+jKSKHImhgyOG70WEXcI4S IZ2J2aphVbvBfGxz3L8GPQiDpq0nuVw= Date: Wed, 5 Jan 2022 13:19:11 +0100 MIME-Version: 1.0 Content-Language: en-US To: pgsql-sql@lists.postgresql.org From: Richard Klingler Subject: Return product category with hierarchical info Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Authentication-Results: ORIGINATING; auth=pass smtp.auth=richard@klingler.net smtp.mailfrom=richard@klingler.net List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Good afternoon (o; First of all, am I am totally no expert in using PGSQL but use it mainly for simple web applications... Now I have a table which represents the categories for products in a hierarchical manner:     id | name | parent So a top category is represented with parent being 0:     1 | 'Living' | 0 The next level would look:     2 | 'Decoration' | 1 And the last level (only 3 levels):     3 | 'Table' | 2 So far I'm using this query to get all 3rd level categories as I use the output for datatables editor as a product can only belong to the lowest category: select id, name from category where parent in (select id from category where parent in (select id from category where parent = 0)) But this has a problem as more than one 3rd level category can have the same name, therefore difficult to distinguish in the datatables editor which one is right. So now my question (finally ;o): Is there a simple query that would return all 3rd levels category ids and names together with the concatenated names of the upper levels? Something like:     3 | 'Table' | 'Living - Decoration' thanks in advance richard PS: If someone could recommend a good ebook or online resource for such stupid questions, even better (o;