| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 16:16:53
|
| Hi I have a table like -auto_id - employee_id - employee_groupIs there a qury that i can run, to check if a employee_id and employee_group have the same content, and if it does, just delete one of the reocrds?Sample Data1 - joe1 - cool_group2 - joe1 - cool_group_13 - joe1 - cool_group4 - joe2 - cool_group_4After Query Run1 - joe1 - cool_group2 - joe1 - cool_group_14 - joe2 - cool_group_4 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-19 : 17:04:44
|
try this:delete from MyTable where auto_id = (select max(auto_id)from MyTablegroup by employee_id, employee_grouphaving count(*) > 1) |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 17:08:00
|
| ErrorMsg 512, Level 16, State 1, Line 1Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The statement has been terminated.table name hr_staff_groupscolums, id - employee_id - employee_group |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-19 : 17:13:15
|
| from your error message it looks like that you have several duplicated records with same employee_id and employee_group? |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 17:19:51
|
| yes,a row can have same employee_id and employee_groupI just want to remove 1 of the records, not bothSample Data1 - joe1 - cool_group2 - joe1 - cool_group_13 - joe1 - cool_group4 - joe2 - cool_group_4After Query Run1 - joe1 - cool_group2 - joe1 - cool_group_14 - joe2 - cool_group_4 |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-19 : 17:20:13
|
| you can solve this problem by setting the auto_id as identity column. |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-19 : 17:21:50
|
this is exact copy of your data and if works:create table #hr_staff_groups(auto_id int,employee_id varchar(10),employee_group varchar(10))insert into #hr_staff_groupsselect 1, 'AB','G1' union allselect 2, 'AB','G2' union allselect 3, 'AB','G1' union allselect 4, 'AD','G3' union allselect 5, 'AB','G1' union allselect 6, 'DC','G1'delete from #hr_staff_groupswhere auto_id = (select max(auto_id)from #hr_staff_groupsgroup by employee_id, employee_grouphaving count(*) > 1) |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 17:23:21
|
| The auto ID is set to identity, and PK, its auto increment. |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-19 : 17:24:16
|
| so query must work.is there any other column(s) in the table? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-07-19 : 17:26:50
|
| [code]-- Setup DataDECLARE @Foo TABLE (auto_id INT, employee_id VARCHAR(50), employee_group VARCHAR(50))INSERT @FooSELECT 1, 'joe1', 'cool_group'UNION ALL SELECT 2, 'joe1', 'cool_group_1'UNION ALL SELECT 3, 'joe1', 'cool_group'UNION ALL SELECT 4, 'joe2', 'cool_group_4'-- Delete DuplicatesDELETE DFROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY employee_id, employee_group ORDER BY auto_id) AS RowNum FROM @Foo ) AS DWHERE RowNum > 1 -- Verify results SELECT *FROM @Foo[/code] |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 17:30:10
|
| perfect Lamprey, that worked great. |
 |
|
|
|