Re: simple? join

Поиск
Список
Период
Сортировка
От Peter Eisentraut
Тема Re: simple? join
Дата
Msg-id Pine.LNX.4.30.0201060020420.2868-100000@peter.localdomain
обсуждение исходный текст
Ответ на simple? join  (Frank Bax <fbax@sympatico.ca>)
Ответы Re: simple? join  (Frank Bax <fbax@sympatico.ca>)
Список 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@gmx.net



В списке pgsql-sql по дате отправления:

Предыдущее
От: Andrew Perrin
Дата:
Сообщение: Which date/paper pairs are NOT represented?
Следующее
От: "Hunter, Ray"
Дата:
Сообщение: Postgres vs. Redhat DB