Re: simple? join

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Frank Bax <fbax(at)sympatico(dot)ca>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: simple? join
Date: 2002-01-06 05:22:10
Message-ID: Pine.LNX.4.30.0201060020420.2868-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Frank Bax writes:

> EMPLOYEE table contains: emp, first, last (emp is unique key)
> TIMESHEET table contains: emp, timestamp, hours
>
> I want to report employee name and total hours.
>
> So far, I've come up with:
>
> SELECT min(emp.first) as first, min(emp.last) as last, sum(ts.hours)
> FROM timesheet ts, employee emp
> WHERE ts.emp = emp.emp
> GROUP by emp.emp
> ORDER BY last, first;
>
> It seems silly to specify "min" when I know there is only one value per emp!

Indeed this looks strange.

> SELECT first, last, sum(ts.hours)
> FROM timesheet ts, employee emp
> WHERE ts.emp = emp.emp
> GROUP by emp.emp, first, last
> ORDER BY last, first;
>
> It seems silly to specify extraneous "group by" fields.

There's nothing "extraneous" there. Both first and last could be
duplicated, so you need to group by each one.

> Is there a 'better' way to write this SQL?

Not really. The latter is how I would write it.

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Darryl M. Wolski 2002-01-07 05:37:56 nested inner join help
Previous Message Karl Raven 2002-01-05 20:54:07 Re: change null to 0 in SQL script