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
 Selecting min and max of multiple records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tlug
Starting Member

USA
19 Posts

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

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!

Thanks!

James K
Flowing Fount of Yak Knowledge

3719 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.
SELECT
	a.*,
	b.*
FROM
	YourTable a
	OUTER APPLY
	( 
		SELECT TOP 1 * FROM YourTable b
		WHERE b.STATUS = 'Task Approved'
		AND b.DateTimesTask > a.DateTimesTask
		ORDER BY DateTimesTask ASC
	) b
WHERE
	a.EVENT = 'Open-Submitted';
Go to Top of Page

tlug
Starting Member

USA
19 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.

Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 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
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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