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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting min and max of multiple records

Author  Topic 

tlug
Starting Member

21 Posts

Posted - 2013-09-27 : 09:15:50
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-27 : 09:48:52
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

21 Posts

Posted - 2013-09-27 : 13:49:00
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

52326 Posts

Posted - 2013-09-28 : 03:23:19
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
   

- Advertisement -