grimstoner
Well-Known Member
- Joined
- Nov 27, 2009
- Messages
- 311
- Reaction score
- 0
Given these tables :
Item
-----
Id
Description
Status
CreatedBy
ItemLog
--------
Id
ItemId
NewStatus
TimeStamp
ChangedBy
Where Itemlog.ItemId = Item.Id, and Status = { "Created", "Pended", "Cancelled", "Completed" }...
How would you write a SQL query to generate the following results :
Which will then be showed as a master-detail report. CurrentUser in the second resultset is that last user to change the status. TimePended is the sum of the difference in datetimes between each change of the status from pended -> something.
Item
-----
Id
Description
Status
CreatedBy
ItemLog
--------
Id
ItemId
NewStatus
TimeStamp
ChangedBy
Where Itemlog.ItemId = Item.Id, and Status = { "Created", "Pended", "Cancelled", "Completed" }...
How would you write a SQL query to generate the following results :
Code:
Item Description ChangeDate NewStatus ChangedBy
1 Test1 2012-01-01 Created User1
1 Test1 2012-01-02 Pended User2
1 Test1 2012-01-03 Completed User2
2 Test2 2012-01-01 Created User2
2 Test2 2012-01-02 Pended User3
2 Test2 2012-01-09 Cancelled User1
3 Test3 2012-01-01 Created User1
3 Test3 2012-01-02 Pended User1
Item CurrentUser CurrentStatus CreatedOn TimeToComplete TimePended CompletedDate CancelledDate
1 User2 Completed 2012-01-01 3 days 1 day 2012-01-03 (null)
2 User1 Completed 2012-01-01 9 days 7 days (null) 2012-01-09
3 User1 Pended 2012-01-01 (null) 30 days (null) (null)
Which will then be showed as a master-detail report. CurrentUser in the second resultset is that last user to change the status. TimePended is the sum of the difference in datetimes between each change of the status from pended -> something.
Last edited: