Reputation: 39
I have a mysql database which has a number of tables:
matters
matterid
mattername
state
matterjuncstaff
junked
matterid
staffid
staff
staffid
staffname
lead
(lead can be either Y or N)matterjunactions
matterid
actiondate
Rules
I want to extract a table which shows all matters where their state
is 'Open'
and lists the matterid
, mattername
, the staffname
for the person who is the lead
on the matter
(i.e. has lead='Y'
) and the last actiondate
for the matter
.
I have tried various ways of doing this and believe that it can be done using subqueries but I can’t work out the format for getting these into my query.
Upvotes: 0
Views: 52
Reputation: 3808
It would look like this:
Select m.matterid, m.mattername, s.staffname, max(actiondate)
From matters m
Inner join matterjuncstaff mjs using (matterid)
Inner join staff s using (staffid)
Inner join matterjunactions mj
On mj.Matterid = m.matterid
Where m.state = 'open'
And s.lead = 'Y'
Group by m.matterid, m.mattername, s.staffname
Upvotes: 1