SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 rows become columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ptah
Starting Member

3 Posts

Posted - 01/18/2007 :  08:05:17  Show Profile  Reply with Quote
hello all,
i am trying to create a view from a table that will keep track of the time between each stage of tasks given. take a look at the data below:


progressID taskID stage status theDate
------------------------------------------------------------------------
1407525 1091657 In Progress Logged 2006-11-16 10:00:24.000
1407526 1091657 In Progress Inprogress 2006-11-16 12:08:59.036
1407214 1091657 In Progress Resolved 2006-11-16 14:15:48.000
1407220 1091657 Closed Solved 2006-11-16 14:36:05.000


i would like to be able to have just one row per task ID showing the difference between the stages, as shown below . only the 2nd column is a date, the rest are are hours (datediff) between the stage and its preceeding stage :

taskID Logged InProgress Resolved Solved
1091657 2006-11-16 10:00:24.000 2.08 2.07 0.21


is it possible to achieve such a transformation using views and a number of select statements (i.e no dts)? all assistance will be highly appreciated.

regards,

ptah

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 01/18/2007 :  08:23:44  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT		TaskID,
		Logged,
		CAST(DATEDIFF(minute, Logged, InProgress) / 60.0 AS NUMERIC(9, 2)) AS InProgress,
		CAST(DATEDIFF(minute, InProgress, Resolved) / 60.0 AS NUMERIC(9, 2)) AS Resolved,
		CAST(DATEDIFF(minute, Resolved, Solved) / 60.0 AS NUMERIC(9, 2)) AS Solved
FROM		(
			SELECT		TaskID,
					MAX(CASE WHEN Status = 'Logged' THEN theDate END) AS Logged,
					MAX(CASE WHEN Status = 'InProgress' THEN theDate END) AS InProgress,
					MAX(CASE WHEN Status = 'Resolved' THEN theDate END) AS Resolved,
					MAX(CASE WHEN Status = 'Solved' THEN theDate END) AS Solved
			FROM		YourTableNameHere
			GROUP BY	TaskID
		) AS d
ORDER BY	TaskID

Peter Larsson
Helsingborg, Sweden

Edited by - SwePeso on 01/18/2007 08:26:00
Go to Top of Page

ptah
Starting Member

3 Posts

Posted - 01/18/2007 :  10:59:14  Show Profile  Reply with Quote
thanks for the help. much appreciated.

ptah
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000