pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Joolz <joolz@arbodienst-limburg.nl>
Cc: pgsql-php@postgresql.org
Subject: Re: grouping query results
Date: Mon, 28 Feb 2005 12:30:51 -0600
Message-ID: <20050228183051.GB27212@wolff.to> (raw)
In-Reply-To: <36567.10.0.4.254.1109584103.squirrel@webmail.arbodienst-limburg.nl>
References: <36567.10.0.4.254.1109584103.squirrel@webmail.arbodienst-limburg.nl>

On Mon, Feb 28, 2005 at 10:48:23 +0100,
  Joolz <joolz@arbodienst-limburg.nl> wrote:
> Hello everyone,
> 
> In a PHP / Postgresql application I have to make a lot of lists like
> this:
> 
> THE DATA STRUCTURE
> employee_number, employee_name, department
> 
> THE OUTPUT
> ------------------------------------
> employee_number  employee_name
> 
> department X
> 1                Johnson
> 22               Jackson
> subtotal: 2 employees
> 
> department Y
> 222              Smith
> 3                Doe
> 44               Jameson
> subtotal: 3 employees
> 
> grand total: 5 employees
> ------------------------------------
> 
> I see 2 ways to solve this:
> 
> - use SELECT for the detail lines and SELECT COUNT (*) for the (sub)
>   totals
> 
> - SELECT the whole lot and let PHP do the grouping and counting

You can use ORDER BY to do most of the grouping work. The app just
needs to check when the department changes and keep counters.

> The second solution seems old fashioned to me and the first has a
> catch: all queries have to be made within a single transaction.

I think more than that. I think you also want the transaction isolation
level set to serializeable if you want a consistant report.

> I have the feeling that this is a very common question, how do you
> people handle it? I looked at a lot of script sites, but no luck.

I would suggest method 2 using an ORDER BY to produce the detail lines
in the correct order.



view thread (4+ messages)

Message-ID: <20050228183051.GB27212@wolff.to>
Permalink:  ../20050228183051.GB27212@wolff.to/
Also on:    postgresql.org/message-id/20050228183051.GB27212@wolff.to

 · 

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-php@postgresql.org
  Cc: bruno@wolff.to, joolz@arbodienst-limburg.nl
  Subject: Re: grouping query results
  In-Reply-To: <20050228183051.GB27212@wolff.to>
  References: <36567.10.0.4.254.1109584103.squirrel@webmail.arbodienst-limburg.nl> <20050228183051.GB27212@wolff.to>

* 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