I can certainly point you in the right direction.
Every state change is logged in htblhistory. In order to get the total time a ticket was in a particular state, you would have to count the time difference between the time stamp of that state and the next timestamp in the table.
While in theory this sounds simple, its not that simple to put this in an SQL query. Personally I've never tried this either, so hopefully someone with more SQL knowledge can chime in. Otherwise google is your best friend.