Re: Database/Table Design for Global Country Statistics - Mailing list pgsql-general

From Stefan Schwarzer
Subject Re: Database/Table Design for Global Country Statistics
Date
Msg-id C692D47F-E08A-4638-8BB0-74519719A213@grid.unep.ch
Whole thread Raw
In response to Re: Database/Table Design for Global Country Statistics  (Richard Huxton <dev@archonet.com>)
Responses Re: Database/Table Design for Global Country Statistics
List pgsql-general
>> Uiuiui.... and it gets even worse... I want to implement the
>> possibility to calculate on-the-fly the per Capita values for the
>> selected data set. With the "old" table design it would be
>> something like this:
>>       SELECT (fish_catch.y_1970 / pop_total.y_1970),
>> (fish_catch.y_1971 / pop_total.y_1971) FROM .....
>
> Or, if the fish + population data are in different tables:
>
> SELECT
>   f.year,
>   f.country,
>   f.fish_catch
>   p.pop_total
>   (f.fish_catch / p.pop_total) AS fish_per_capita
> FROM
>   fish_figures f
> JOIN
>   popn_figures p
> USING (year, country)
> ORDER BY f.year, f.country;
>

Muchos gracias, Mr. Postgres! I am really touched by your help!

Just for the completeness, I attach the final working SQL query:

SELECT
   f.year,
   f.id,
   c.name,
   (f.value / p.value) AS per_capita
FROM
   fish_catch AS f
JOIN
   pop_total AS p
USING
    (year, id)
INNER JOIN
    countries AS c ON f.id = c.id
ORDER BY
    year

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Cannot declare record members NOT NULL
Next
From: Richard Huxton
Date:
Subject: Re: Database/Table Design for Global Country Statistics