Finding Largest Dates for each matching foreign key. SQL

I am trying to produce an SQL select statement that will extract the highest date value for each category.
The attached image best demonstrates the desired output.
There are more categories in the production system although I am only interested in a subset. Equally there are millions of records in the TblActions table.  The highest ActionID of each value against each company could be used to determine the most recent as the DateOfAction is not updated and is created at the time of the record being inserted.  This may be a faster route for determining the result.
At the moment I use multiple joins, one for each action I seek.  I have found this to be quite sluggish, even with indexes.  Can you please recommend an approach that would best facilitate getting the desired result.  I am using SQL 2000 at the moment so need an SQL 2000 compatible solution although this is in migration to SQL 2012 so I would certainly be interested in hearing about any methods that would make this faster in newer versions if there are any.
I have also attached the spreadsheet for copying/pasting if needed.
Thank you so much!
MaxVals2.png
data.xls
dgloverukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awking00Commented:
Quick question - Wouldn't the meeting value for CompanyID 2 be 5/21/2013?
0
dgloverukAuthor Commented:
Sorry, Awking yes you are correct, it should be 5/21/2013, glad you understood it well enough to correct me :)
And sorry Jim, I will endeavour to use this in future.
0
awking00Commented:
The following should work in sql server 2000 -
select companyid,
max(case when actionid = 1 then dt end) as phonecall,
max(case when actionid = 2 then dt end) as email,
max(case when actionid = 3 then dt end) as conference,
max(case when actionid = 4 then dt end) as meeting
from
(select a.companyid, a.actionid, b.actionname, max(dateofaction) dt
 from tblactions a inner join tblaction b on a.actionid = b.actionid
 group by a.companyid, a.actionid, b.actionname
 order by a.companyid, a.actionid, b.actionname) x
group by companyid
order by companyid;

When you get to sql server 2012, you may want to read up on pivot and window functions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgloverukAuthor Commented:
Thank you Awking, this is just what I needed!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.