SELECT multiple MAX(id)s ?

From: Aarni Ruuhimäki <aarni(at)kymi(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: SELECT multiple MAX(id)s ?
Date: 2008-10-10 10:56:11
Message-ID: 200810101356.11539.aarni@kymi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello list,

table diary_entry

entry_id SERIAL PK
d_entry_date_time timestamp without time zone
d_entry_company_id integer
d_entry_location_id integer
d_entry_shift_id integer
d_user_id integer
d_entry_header text
...

Get the last entries from companies and their locations?

The last, i.e. the biggest entry_id holds also the latest date value within
one company and its locations. One can not add an entry before the previuos
one is 'closed'. Names for the companies, their different locations, or
outlets if you like, users and shifts are stored in company, location, user
and shift tables respectively.

Again something I could do with a bunch of JOIN queries and loops + more LEFT
JOIN queries within the output loops, but could this be done in a one single
clever (sub select?) query?

Output (php) should be something like:

Date | User | Shift | Company | Location
---------------------------------------------------------

02.10.2008 | Bobby | Nightshift 1 | Company 1 | Location X
04.10.2008 | Brian | Dayshift 2 | Company 1 | Location Y
09.10.2008 | Jill | Dayshift 1 | Company 2 | Location A
05.10.2008 | Jane | Dayshift 1 | Company 2 | Location B
07.10.2008 | Frank | Dayshift 2 | Company 2 | Location C
...

Someone please give me a start kick?

TIA and have a nice weekend too!

--
Aarni

Burglars usually come in through your windows.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Sabin Coanda 2008-10-10 11:25:55 Re: 100% CPU at concurent access
Previous Message Bart Degryse 2008-10-10 07:06:00 Re: trigger parameters, what am I doing wrong ??