Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cOwok-0001ix-TI for pgsql-bugs@arkaria.postgresql.org; Thu, 05 Jan 2017 01:26:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cOwok-0005kw-6K for pgsql-bugs@arkaria.postgresql.org; Thu, 05 Jan 2017 01:26:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cOwmY-0004oN-4N for pgsql-bugs@postgresql.org; Thu, 05 Jan 2017 01:23:54 +0000 Received: from mx1.riseup.net ([198.252.153.129]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cOwmT-00026K-Pr for pgsql-bugs@postgresql.org; Thu, 05 Jan 2017 01:23:53 +0000 Received: from piha.riseup.net (unknown [10.0.1.163]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client CN "*.riseup.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (verified OK)) by mx1.riseup.net (Postfix) with ESMTPS id 7BB241A1A17 for ; Thu, 5 Jan 2017 01:23:46 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: nnj) with ESMTPSA id E87EF1C007A To: pgsql-bugs@postgresql.org From: Gwork Subject: Postgresql query HAVING do not work Message-ID: <7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net> Date: Thu, 5 Jan 2017 01:23:43 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -5.8 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Version: Postgresql 9.5 OS: Debian 8 jessie run on docker Following this tutorial The Nested Set Model on http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ Section: Depth of a Sub-Tree. SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name, node.lft ORDER BY node.lft )AS sub_tree WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.name GROUP BY node.name, node.lft, sub_tree.depth ORDER BY node.lft; +----------------------+---------+ | name | depth | |----------------------+---------| | PORTABLE ELECTRONICS | 0 | | MP3 PLAYERS | 1 | | FLASH | 2 | | CD PLAYERS | 1 | | 2 WAY RADIOS | 1 | +----------------------+---------+ Section: Find the Immediate Subordinates of a Node. SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name, node.lft ORDER BY node.lft )AS sub_tree WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.name GROUP BY node.name, node.lft, sub_tree.depth HAVING depth <= 1 ORDER BY node.lft; Adding 'HAVING depth <= 1' to the query still return the same results as above instead of this: +----------------------+---------+ | name | depth | |----------------------+---------| | PORTABLE ELECTRONICS | 0 | | MP3 PLAYERS | 1 | | FLASH | 1 | | CD PLAYERS | 1 | | 2 WAY RADIOS | 1 | +----------------------+---------+ I don't know if I'm doing anything wrong? Note: Edit the post query by adding node.lft, sub_tree.depth to the GROUP BY. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs