pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gwork <nnj@riseup.net>
To: pgsql-bugs@postgresql.org
Subject: Postgresql query HAVING do not work
Date: Thu, 5 Jan 2017 01:23:43 +0000
Message-ID: <7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (6+ messages)  latest in thread

Message-ID: <7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net>
Permalink:  ../7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net/
Also on:    postgresql.org/message-id/7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: nnj@riseup.net
  Subject: Re: Postgresql query HAVING do not work
  In-Reply-To: <7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net>
  References: <7582ea1e-6146-fd8d-b564-c2fe251210b2@riseup.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox