Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table and i want to delete only those rocords from the table where WorkType ='Non Production' and no any other workType will associate with that EmployeeCode with same project Name.

EmpCodeProjectNameWorktypeDuration
101ABCNon Production2
101ABCQA10
102BCDNon Production8
103BCDQA5
104ABCNon Production7
103ABCNON Production5


The result would be like this:-

EMPCodeProjectNameWorkTypeDuration
102BCDNON Production8
101ABCQA10
103BCDQA5
104ABCNon Production7
103ABCNOn Production5


Here you can see that Firstrow from table 1 is getting deleted with EmpCode=101 remaining are as it is in the result . Emp Code=102, 104 and 103 also have Worktype as Non Production but they have another records also underlying with same Emp Code, Same projectname but differnt Worktype .

Please help me and share me the query to solve this Problem.
Posted
Updated 22-Feb-12 0:20am
v3
Comments
OriginalGriff 22-Feb-12 6:00am    
I don't see from your sample data that EMPCode 104 has any other records - it is not obvious why it should remain and 102 should be deleted.
Use the "Improve question" widget to edit your question and provide better information.
Dharmenrda Kumar Singh 22-Feb-12 6:22am    
Thanks . I had updated the Question and thanks for pointing my mistake.
OriginalGriff 22-Feb-12 7:23am    
Um. 104 still looks to be deletable if 102 is. It does not share an Emp code, it does share a project name, and it has a different Worktype.
Dharmenrda Kumar Singh 22-Feb-12 7:26am    
We have to delete only those worktype which have another record with same EmpCode, Same Project name but differnt WorkType.104 Emp Code doest have any other record with same project name,Same EmpCode but different Worktype so we had not deleted it.
Varun Sareen 22-Feb-12 7:46am    
it worked??

1 solution

try this:-
With tb as
(
Select ROW_NUMBER() over(PARTITION by Emp_Code,ProjectName order by emp_code,Worktype desc) 
	as Srn,* From TestEmp 
) Delete From tb Where Srn>1 And WorkType='Non Production'


and reply if it works or not.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900