From: Alexander Gataric <gataric@usa.net>
To: pgsql-sql@postgresql.org
Subject: Re: Summing & Grouping in a Hierarchical Structure
Date: Thu, 14 Feb 2013 22:30:47 -0600
Message-ID: <001601ce0b35$3a395090$aeabf1b0$@net> (raw)
In-Reply-To: <CAJ-7yo=cpz0TocY4Qvb5e4=s3FB+VtHT3VV3gbUAz8Uqi7p6pw@mail.gmail.com>
References: <CAJ-7yo=cpz0TocY4Qvb5e4=s3FB+VtHT3VV3gbUAz8Uqi7p6pw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
I would try a recursive
<http://www.postgresql.org/docs/8.4/static/queries-with.html; query to
determine the category structure and aggregate as you go. I had a similar
problem with a hierarchical structure for an organization structure. Another
thing you might try is to create a separate CTE for each category and then
aggregate the individual CTEs.
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Don Parris
Sent: Thursday, February 14, 2013 7:58 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Summing & Grouping in a Hierarchical Structure
Hi all,
I posted to this list some time ago about working with a hierarchical
category structure. I had great difficulty with my problem and gave up for
a time. I recently returned to it and resolved a big part of it. I have
one step left to go, but at least I have solved this part.
Here is the original thread (or one of them):
http://www.postgresql.org/message-id/CAJ-7yonw4_qDCp-ZNYwEkR2jdLKeL8nfGc+-TL
LSW=Rmo1VkbA@mail.gmail.com
Here is my recent blog post about how I managed to show my expenses summed
and grouped by a mid-level category:
http://dcparris.net/2013/02/13/hierarchical-categories-rdbms/
Specifically, I wanted to sum and group expenses according to categories,
not just at the bottom tier, but at higher tiers, so as to show more
summarized information. A CEO primarily wants to know the sum total for all
the business units, yet have the ability to drill down to more detailed
levels if something is unusually high or low. In my case, I could see the
details, but not the summary. Well now I can summarize by what I refer to
as the 2nd-level categories.
Anyway, I hope this helps someone, as I have come to appreciate - and I mean
really appreciate - the challenge of working with hierarchical structures in
a 2-dimensional RDBMS. If anyone sees something I should explain better or
in more depth, please let me know.
Regards,
Don
--
D.C. Parris, FMP, Linux+, ESL Certificate
Minister, Security/FM Coordinator, Free Software Advocate
http://dcparris.net/ <https://www.xing.com/profile/Don_Parris;
GPG Key ID: F5E179BE
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: gataric@usa.net
Subject: Re: Summing & Grouping in a Hierarchical Structure
In-Reply-To: <001601ce0b35$3a395090$aeabf1b0$@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