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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

IK1972
Yak Posting Veteran

52 Posts

Posted - 03/13/2013 :  14:56:48  Show Profile  Reply with Quote

drop table #temp
Create table #temp(TranID int, TaskID int, TaskName varchar(255), TaskStatus varchar(25))

insert into #temp values
(1,1,'A1','Created'),
(1,2,'A2','Created'),
(1,3,'A3','Created'),
(1,4,'A4','Created'),
(1,5,'A5','Created'),
(2,6,'A1','Completed'),
(2,7,'A2','Created'),
(2,8,'A3','Created'),
(2,9,'A4','Created'),
(2,10,'A5','Created'),
(3,11,'A1','Completed'),
(3,12,'A2','Completed'),
(3,13,'A3','Completed'),
(3,14,'A4','Created'),
(3,15,'A5','Created'),
(4,16,'A1','Completed'),
(4,17,'A2','Completed'),
(4,18,'A3','Completed'),
(4,19,'A4','Completed'),
(4,20,'A5','Created'),
(5,21,'A1','Created'),
(5,22,'A2','Created'),
(5,23,'A3','Completed'),
(5,24,'A4','Created'),
(5,25,'A5','Created')


Basically for every TranID we have 5 Tasks.

My Requirement is for any TranID check the first task which is created and then cancel all others if its not completed.

For TranID 1 Task ‘A1’ is Created so we can cancel all others means (A2 to A5)

For TranID 2 First Created Task is A2 so we can cancel (A3 to A5)

For TranID 3 First Created Task is A4 so we can cancel only (A5)

For TranID 4 First Created Task is A5 and after that we don’t have any task so no update

For TranID 5 Task A1 is Created and Task A3 is completed so we can cancel Task (A2,A4,A5)

Thanks

djj55
Constraint Violating Yak Guru

USA
337 Posts

Posted - 03/13/2013 :  15:55:17  Show Profile  Reply with Quote
;with cte as (select TranID, MAX(TaskID) AS MTID FROM #Temp GROUP BY TranID) 
DELETE T FROM #Temp T INNER JOIN cte on t.TranID = cte.TRANID and T.TaskID > cte.MTID

Not tested but it might give you some ideas.

djj
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
412 Posts

Posted - 03/14/2013 :  12:56:05  Show Profile  Reply with Quote

DELETE FROM temp
FROM #temp temp
INNER JOIN (
    SELECT
        TranID, 
        MIN(CASE WHEN TaskStatus = 'Created' THEN TaskID ELSE 999999 END) AS min_created_taskid
    FROM #temp temp2
    GROUP BY
        TranID
) AS temp_control ON
    temp.TranID = temp_control.TranID AND
    temp.TaskID > temp_control.min_created_taskid AND
    temp.TaskStatus <> 'Completed'

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.06 seconds. Powered By: Snitz Forums 2000