Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting min and max of multiple records
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

21 Posts

Posted - 09/27/2013 :  09:15:50  Show Profile  Reply with Quote

I have an issue with some data.. I have been working on this for some time and can't get the query to pickup the information the way I need it..

Below is a sample of my data.. What I need is the start and end time of each task, but the issue is there is no unique task number to bind them together.. So for instance the task starts with 'Open-Submitted' and ends with 'Task Approved'. The issue is there can be multiple occurences in the same file number. I need to be able to split these into multiple tasks with the associated start and stop times.

File ID		Datetimes	Task Event Status	Task Event Name		Task ID		Event ID
File 1		6/3/13 16:33	Open-Submitted		Task is retrieved		TSK-123456	12345
File 1		6/3/13 16:44	Open-Approved		Task Approved			TSK-123456	23456
File 1		6/20/13 18:11	Open-Submitted		Task is retrieved		TSK-123456	34567
File 1		6/21/13 14:42	Open-Approved		Task Approved			TSK-123456	45678

Thank you for the help!


James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 09/27/2013 :  09:48:52  Show Profile  Reply with Quote
This should get you a set of rows with Event = Open-Submitted and the next Task Approved.
	YourTable a
		SELECT TOP 1 * FROM YourTable b
		WHERE b.STATUS = 'Task Approved'
		AND b.DateTimesTask > a.DateTimesTask
		ORDER BY DateTimesTask ASC
	) b
	a.EVENT = 'Open-Submitted';
Go to Top of Page

Starting Member

21 Posts

Posted - 09/27/2013 :  13:49:00  Show Profile  Reply with Quote
Thanks for the reply james! I should have put this in my post, the database is Teradata.. Based on what I am reading Outer Apply or Apply doesn't work in Teradata.

Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 09/28/2013 :  03:23:19  Show Profile  Reply with Quote
Then you've to try your luck at some Teradata forums. This is MS SQL Server forums and we dont have much experts on Teradata here

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next 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