Re: grouping query results

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Joolz <joolz(at)arbodienst-limburg(dot)nl>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: grouping query results
Date: 2005-02-28 18:30:51
Message-ID: 20050228183051.GB27212@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

On Mon, Feb 28, 2005 at 10:48:23 +0100,
Joolz <joolz(at)arbodienst-limburg(dot)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.

In response to

Responses

Browse pgsql-php by date

  From Date Subject
Next Message operationsengineer1 2005-03-01 19:14:35 PHP and Error Reporting
Previous Message Mitch Pirtle 2005-02-28 18:14:38 Re: grouping query results