Re: [GENERAL] Problem with aggregates and group by

From: Ulf Mehlig <umehlig(at)uni-bremen(dot)de>
To: szoli(at)netvisor(dot)hu
Cc: pgsql-general(at)postgreSQL(dot)org
Subject: Re: [GENERAL] Problem with aggregates and group by
Date: 1998-10-30 16:47:31
Message-ID: 199810301647.RAA07593@uni-bremen.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sebestyn Zoltn AKA Memphisto wrote:

> select annex_log.username,
> annex_log.login_time,annex_log.bytes_in, annex_log.bytes_out
> from users, groups, annex_log
> where
> ppp_start >= date_trunc('day','1998 September 20'::datetime) and
> ppp_start < (date_trunc('day','1998 September 20'::datetime) +
> '1 day'::timespan)
> group by username
>
> and found out that that query returned the very same lines a lot of
> times. It can be a side effect of join.
> So I included a 'distinct' in the second query and it finally gave me the
> correct lines, but how can I to that when using aggregates, too?

When you join two tables, the database program makes pairs of *each*
row from the first table with *every* row from the second. Normally
you will specify a condition (e.g. "where table1.xxx = table2.yyy") to
limit the output to those rows of the result table, where two key
columns have equal values. You did some kind of a join between the
three tables by specifying them in the "from" phrase, but you did not
give the advise how to join them in the "where" clause. The database
now joins really every row with all the others ... BTW, why do you
join the three tables in this query? You are querying columns from the
annex_log-table only ;-)

Unfortunately, I can't recommend a good book about SQL since I have
read only the user manuals of the commercial database systems I had to
work with; I know a rather theoretical textbook (Database System
Concepts, H. F. Korth & A. Silberschatz, McGraw-Hill 1991) which
explaines a bit of SQL (shortly!) and gives a (theoretical!)
introduction of relational operations like joins. Maybe you find it in
a library where you can copy the interesting pages ... But I'm sure
there are people reading the list who can give better tips ;-)

Hope it helps,
Ulf

--
======================================================================
%%%%% Ulf Mehlig <ulf(dot)mehlig(at)uni-bremen(dot)de>
%%%%!%%% Projekt "MADAM" <umehlig(at)uni-bremen(dot)de>
%%%% %!% %%%% ----------------------------------------------------
---| %%% MADAM: MAngrove | Center for Tropical Marine
||--%!% Dynamics | Biology
|| And | Fahrenheitstrasse 1
_ /||\_/\_ Management |
/ / \ \ ~~~~~~~~~~~~~~~~~ | 28359 Bremen/Germany
~~~~~~~~~~~~~~~~~~~~

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Memphisto 1998-10-30 17:13:13 Re: [GENERAL] Problem with aggregates and group by
Previous Message Lee Roth 1998-10-30 16:28:45 backslash D in psql fails