Re: Database/Table Design for Global Country Statistics

From: Richard Huxton <dev(at)archonet(dot)com>
To: Stefan Schwarzer <stefan(dot)schwarzer(at)grid(dot)unep(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Database/Table Design for Global Country Statistics
Date: 2007-09-12 09:45:18
Message-ID: 46E7B52E.7070608@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Stefan Schwarzer wrote:
> Thanks for the feedback and the suggestions.
>
> A problem I have now when using the new design is the following:
>
> As a result from my PostGres query I get something like this:
>
> year | value | name
> ---------------------------------------
> 2001 | 123 | Afghanistan
> 2002 | 125 | Afghanistan
> 2003 | 128 | Afghanistan
[etc]
> The way it is displayed on the web (in table form) is the "usual" way:
>
> name 2001 2002 2003 2004 2005
> -----------------------------------------------------------------
> Afghanistan ....
> Albania ....
>
>
> Is there any "simple", elegant solution for PHP, so that it does this
> transformation? I can't imagine that I would have to write a couple of
> IFs to achieve that. But I have no other idea. Or is it a question of
> writing a more elegant SQL query?

$curr_yr = -1
$cols = array();
while (<fetch rows>) {
if ($row['year'] != $curr_yr) {
if (sizeof($cols) > 0) { display_table_row($cols); }
$cols = array();
$curr_year = $row['year'];
}
$cols[] = $row['value'];
}
// handle possible last row of table
if (sizeof($cols) > 0) { display_table_row($cols); }

Make sure your query is ordered properly and you don't have gaps in your
years.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2007-09-12 09:53:40 Re: Table partitioning based on multiple criterias possible?
Previous Message Ow Mun Heng 2007-09-12 09:39:32 Re: Table partitioning based on multiple criterias possible?