Re: I need help creating a query

Поиск
Список
Период
Сортировка
От Richard Broersma Jr
Тема Re: I need help creating a query
Дата
Msg-id 20060713194444.59574.qmail@web31806.mail.mud.yahoo.com
обсуждение исходный текст
Ответ на I need help creating a query  ("Sergio Duran" <sergioduran@gmail.com>)
Ответы Re: I need help creating a query  ("Sergio Duran" <sergioduran@gmail.com>)
Список pgsql-general
>
> worker:   worker_id, name
> position: position_id, worker_id, position, startdate, salary
>
> If I perfom a query joining both tables, I can obtain all the workers and
> the positions the've had.
>
> SELECT name, startdate, position,  salary FROM worker JOIN position
> USING(worker_id);
> worker1 | 2001-01-01 | boss             | 999999
> worker2 | 2001-01-01 | cleaning        | 100
> worker2 | 2006-04-01 | programmer   | 20000
> worker2 | 2006-07-04 | management | 25000
>
> so far so good, now I need to obtain all the workers only with the position
> they had on a given date.
> if I wanted to know the positions on '2006-05-01' it would return
> worker1 | 2001-01-01 | boss             | 999999
> worker2 | 2006-04-01 | programmer   | 20000
>

This is just a quick guess. I am not sure if the logic is correct but it could be a starting
point.

select P2.worker_id, P2.pdate, P1.position, P1.salary
from position as P1
  join
  (select worker_id, max(startdate) as pdate
   from position
   where startdate <= '2006-05-01'
   group by worker_id, position_id) as P2
on (P1.worker_id = P2.worker_id) and (P1.startdate = P2.pdate)
;

Regards,

Richard Broersma Jr.

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

Предыдущее
От: "Marco Bizzarri"
Дата:
Сообщение: Clustering and backup with large objects
Следующее
От: "Merlin Moncure"
Дата:
Сообщение: Re: Is there a way to run tables in RAM?