logo       

Re: grouping query results: msg#00037

Subject: Re: grouping query results
On Mon, 2005-02-28 at 10:48 +0100, Joolz 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
> 
> 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 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 tend to use the second solution purely for performance reasons
since the first solution will require a select plus one select per
department, which won't scale well to lots of departments.

function print_total( $label, $total ) {
  echo "%s: %d employees";
}

SELECT employee_number, employee_name, department FROM xxx ORDER BY
department

$gtotal = 0;
$dtotal = 0;
$last_department = "no department";
for( $i=0 $i < rows; $row = pg_Fetch_Object(..., $i) {
  if ( $row->department != $last_department ) {
    if ( $i > 0 ) {
      print_total( "subtotal", $dtotal );
    }
    $dtotal = 0;
    $last_department = $row->department;
  }
  printf( "%5d   %s", $row->employee_number, $row->employee_name );
  $dtotal++;
  $gtotal++;
}

print_total( "subtotal", $dtotal );
print_total( "grand total", $gtotal );



Cheers,
                                        Andrew McMillan.

-------------------------------------------------------------------------
Andrew @ Catalyst .Net .NZ  Ltd,  PO Box 11-053, Manners St,  Wellington
WEB: http://catalyst.net.nz/            PHYS: Level 2, 150-154 Willis St
DDI: +64(4)803-2201      MOB: +64(272)DEBIAN      OFFICE: +64(4)499-2267
          What are they doing now? http://schoolreunions.co.nz/
-------------------------------------------------------------------------

Attachment: signature.asc
Description: This is a digitally signed message part

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
audio.irate.dev...    yellowdog.gener...    ietf.ips/2002-0...    xfree86.fonts/2...    busybox/2003-07...    emacs.jdee/2004...    linux.mandrake....    hardware.microc...    user-groups.lin...    science.analysi...    version-control...    db.filemaker.de...    cluster.openmos...    mail.eyebrowse....    text.xml.xerces...    kde.devel.kwrit...    finance.moneyda...    gcc.regression/...    network.routing...    os.freebsd.deve...    recreation.radi...    qnx.openqnx.dev...    python.xml/2002...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe