Hi,
I've made database design for a small CRM system. It comprises of Companies and Meetings (amongst others).
Companies has the fields: ID (primary, auto_inc) Name (text)
Meetings has the fields: ID (primary, auto_inc) CompanyId (link to Companies.ID) WhenTime (datetime, to store when the meeting was) Notes (text about the meeting)
What I want to accomplish is a query that gives me a list of all Companies (all fields in the table), AND the WhenTime and Notes of the latest meeting with that company (latest is max(WhenTime), and if there is none, a NULL will do fine).
I think I can solve this with cursors, but I'm afraid of speed.
I've tried several Group By formulations, but I fear I lack the finesse required.
My last attempt was this:
select Companies.ID, Companies.name, mts.whentime, mts.notes
from Companies
left outer join (
select top(1) *
from Meetings
order by [whentime] desc
) mts
on Companies.ID = mts.companyID
order by Companies.name asc
but this code only takes one tuple from Meetings, not one per company in the join, so it's no good.
Any ideas?