Author |
Topic |
IK1972
56 Posts |
Posted - 2013-03-13 : 14:56:48
|
drop table #tempCreate 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 updateFor 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
352 Posts |
Posted - 2013-03-13 : 15:55:17
|
[code];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[/code]Not tested but it might give you some ideas.djj |
 |
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2013-03-14 : 12:56:05
|
[code]DELETE FROM tempFROM #temp tempINNER 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'[/code] |
 |
|
|
|
|