From: | Michael Glaesemann <grzm(at)seespotcode(dot)net> |
---|---|
To: | Steve Crawford <scrawford(at)pinpointresearch(dot)com> |
Cc: | Stefan Schwarzer <stefan(dot)schwarzer(at)grid(dot)unep(dot)ch>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: Find min year and min value |
Date: | 2007-10-02 18:10:49 |
Message-ID: | 49C9F9A7-F165-4702-96E0-FB35EAC79F03@seespotcode.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Oct 2, 2007, at 11:10 , Steve Crawford wrote:
> As others have noted, the query *can* be written. But it appears to me
> that you are struggling against your table layout.
The current schema he has is commonly called EAV (entity-attribute-
value) and is generally frowned upon. Now, in his particular case it
may be justified if the "value" column values are actually all of the
same type, such as currency amounts for each category. If this is the
case, I suggest renaming the column to be more descriptive of what is
actually stored: likewise the id_variable column.
> Before struggling
> with ever more complicated queries, I'd consider restructuring your
> table(s). There are many possibilities depending on the current nature
> of your data, how you expect it to change and the queries you
> expect to
> run against it. For example:
> country_id
> data_year
> gdp
> fish_catch
This would be one way to do it. However, each time you add a new
category you'd need to add a new column to the table: not very
flexible. You can also have the same functionality by adding a new
table for each category:
> Alternately, you could have a gdp table and a fish_catch table which
> would be easily joined to give the same result.
Expanding on this:
create table fish_catches (country text not null,
data_year date not null,
primary key (country, data_year),
fish_catch numeric not null);
create table gdp (country text not null reference countries
data_year date not null,
primary key (country, data_year),
gdp numeric not null);
This makes your queries quite simple:
select country, data_year, fish_catch, gdp
from fish_catches
natural join gdp
where country = :country
order by data_year
limit 1;
or
select country, data_year, fish_catch, gdp
from fish_catches
natural join gdp
natural join (select country, min(data_year) as data_year
from gdp
natural join fish_catch
group by country) min_data_year
where country = :country;
Splitting categories into separate tables also eliminates the
necessity of worrying about NULL, which can lead to unexpected
behavior if you aren't careful.
Michael Glaesemann
grzm seespotcode net
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Davis | 2007-10-02 18:43:37 | Re: Strange behavior of TRIGGER |
Previous Message | Bill Moran | 2007-10-02 18:01:48 | Re: more problems with count(*) on large table |