Let's say I have a table
SURF, columns
S_ID, S_DESC,S_CITY
example data:
1, 'fun wave','Hermosa'
2, 'big wave'.'Maverick'
2, 'micro wave'.'Hermosak'
and an event table for each of this
surf occassions.
SURF_EVENTS, columns
SE_ID, S_ID, SE_CREATION_DATE,SE_DESC
example data
1,1,2001-01-01,'booked'
2,1,2001-01-02,'mailed'
3,1,2001-01-03,'cancelled'
4,2,2001-01-01,'booked'
5,3,2001-02-01,'water bad'
SURF.S_ID is a foreign key to SURF_EVENTS.S_ID.
Indexes are put appropiately.
My goal is to, in one SQL statement,
based on a city name, get the surf id, and the
latest surf event (SE_DESC) based on the last
S_CREATION_DATE for that S_ID
without having full table scans on SURF_EVENTS
If I did this database over again,
I could have had a trigger in SURF_EVENTS
which every time an insert occurred, an extra flag would be set
to 0 for all the old events, and the new event would be flagged
as 1. Then I knew which one would be last one, without using
MAX(SE_CREATION_DATE), but I can't.
Any suggestions on how to construct a SQL without inserting
a trigger?
Regards,
/latompa