From: | "Michael Diener" <m(dot)diener(at)gomogi(dot)com> |
---|---|
To: | "'Richard Broersma'" <richard(dot)broersma(at)gmail(dot)com>, "'Oliveiros'" <oliveiros(dot)cristina(at)marktest(dot)pt>, "'A(dot) Kretschmer'" <andreas(dot)kretschmer(at)schollglas(dot)com>, "'Thom Brown'" <thombrown(at)gmail(dot)com> |
Cc: | <pgsql-novice(at)postgresql(dot)org>, <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: [NOVICE] sum multiple tables gives wrong answer? |
Date: | 2010-06-02 18:45:56 |
Message-ID: | 005501cb0283$d73b4c30$85b1e490$@diener@gomogi.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general pgsql-novice |
I just want to say thanks to all the great responses it is now working like
a charm!! I knew I was missing some tid bit of DB SQL knowledge.
Thanks again!
Cheers
michael
-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Richard Broersma
Sent: Mittwoch, 02. Juni 2010 17:39
To: m(dot)diener(at)gomogi(dot)com
Cc: pgsql-novice(at)postgresql(dot)org; pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] [NOVICE] sum multiple tables gives wrong answer?
On Wed, Jun 2, 2010 at 7:23 AM, Michael Diener <m(dot)diener(at)gomogi(dot)com> wrote:
> select sum(flaeche)/10000 as "greens HA" from green;
> result:
> Wrong Answer with this query
>
> select
> sum(green.flaeche)/10000 as "greens HA",
> sum (fairway.flaeche)/10000 as "fairway HA"
> from green, fairway;
It isn't easy to see but you are effectively joining green to fairway
using a cross project which product a Cartesian product.
you probably wanted this query:
SELECT (select sum(flaeche)/10000 from green) AS "greens HA",
(select sum(flaeche)/10000 from fairway) AS "fairway HA";
However, from what you've shown. I would wager that your database is
in need of some normalization. For example you could put both greens
and fair way into a single table like:
CREATE TABLE Lawns AS
SELECT flaech, "green"::VARCHAR AS lawntype
FROM green
UNION ALL
SELECT flaech, "fairway"::VARCHAR AS lawntype
FROM fairway;
Then you'd execute the following query:
SELECT lawntype, sum(flaech)/10000 AS "HA"
FROM Lawns
GROUP BY lawntype;
--
Regards,
Richard Broersma Jr.
Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug
From | Date | Subject | |
---|---|---|---|
Next Message | Peter Geoghegan | 2010-06-02 18:47:30 | Re: server-side extension in c++ |
Previous Message | Stephen Frost | 2010-06-02 18:40:46 | Re: postgres authentication against Windows Domain |
From | Date | Subject | |
---|---|---|---|
Next Message | u235sentinel | 2010-06-02 20:20:40 | Re: Best starter book |
Previous Message | Stephen Frost | 2010-06-02 17:13:59 | Re: sum multiple tables gives wrong answer? |