I have a database table r_event
with columns:
event_start (date),
event_stop (date),
insurance_id (integer)
and a table r_insurance
with columns:
insurance_id serial primary key,
insurance_name (text)
Each insurance has several events linked by insurance_id
.
I am trying to:
SELECT insurance_id, insurance_name
- only 1 of each,
and order by the biggest event_stop
:
ORDER BY event_stop DESC NULLS LAST
-- ??
EXAMPLE
r_insurance (insurance_id, insurance_name)
1 | rca
2 | casco
3 | itp
r_event (insurance_id, event_start, event_stop)
1 | 12.10.2012 | 27.11.2012
1 | 07.05.2012 | 24.06.2012
2 | 21.01.2013 | 14.02.2013
The output should be:
1 | casco -- cause it has the event with the biggest event_stop 2 | rca -- cause it has the 1st event_stop after the biggest event_stop 3 | itc -- cause it doesn't have events
I edited my first draft, I want them to be ordered descending by the event with the biggest event_stop
and NULLS LAST
.