Bjgrim, RIM bases LastContactTime off LastFwdTime and LastSentTime.
I'm guessing its something along the lines of getting the return from each of the above columns and comparing to see which one is greater, then using it for the base of comparison against the current time on the server and taking the difference and displaying as the LastContactTime.
Now, how to write that into a SQL statement is beyond me.
This leaves a little room for improvement of the actual LastContactTime return for your code, though. I will talk to our SQL DBAs tomorrow when I get some free time and see if I can get them to look at it.