For the following table :
Id Timestamp AssignedTo
1 2012-01-01 User1
2 2012-01-02 User2
3 2012-01-10 User3
4 2012-01-15 User1
what would be the general approach in MS SQL Server to calculating how many days/hours/minutes the entity was assigned to a specific user?
As a developer (with basic SQL knowledge), I've always opted for Functions or Stored procedures when doing these kind of queries (these kind = queries including results based on the difference between two rows in a set).
In this example, I'd have a cursor iterating over the items, keeping the previous one in a variable, and then calculating the difference. However, I've been told that this is a serious performance concern, especially on large sets.
EDIT: Sample results
User TotalTime
User1 23d
User2 8d
User3 5d
i.e. the total time that an item was assigned to a specific user. User1's TotalTime is 23 days because it has been assigned to him since 2012-01-15 (and it's 2012-02-06 now), along with the first day it was assigned to him.