I am currently trying to update a table which contains a large number of duplicate rows, but contains no unique identifier. I want to add a minus sign (-) before the te_system_ref of each duplicate, but leave the original. I have the following query that gives me the full list of duplicates (as shown below) but I can't figure out how I would go about only removing the duplicates.
Current query:
select te_system_ref, te_event, te_Date
from ticket_events
where te_system_ref in (select te_system_ref from ticket_events
where te_event = 'VQ5 hard copy follows'
and te_date between '04/12/17' and '18/06/18 23:59:59'
group by te_system_ref
having count (te_event) > 4)
and te_event = 'VQ5 hard copy follows'
So instead of:
1046338 2018-04-24 07:01:57.000 16210 VQ5 hard copy follows
1046338 2018-04-24 07:01:58.000 16210 VQ5 hard copy follows
1046338 2018-04-25 07:02:49.000 16210 VQ5 hard copy follows
1046338 2018-04-25 07:02:50.000 16210 VQ5 hard copy follows
1064317 2018-03-21 16:21:52.000 16210 VQ5 hard copy follows
1064317 2018-03-27 12:32:16.000 16210 VQ5 hard copy follows
1064317 2018-04-18 07:00:38.000 16210 VQ5 hard copy follows
1064317 2018-04-19 07:00:39.000 16210 VQ5 hard copy follows
1064351 2018-03-21 16:21:47.000 16210 VQ5 hard copy follows
1064351 2018-03-27 12:31:51.000 16210 VQ5 hard copy follows
1064351 2018-04-18 07:01:50.000 16210 VQ5 hard copy follows
1064351 2018-04-19 07:02:03.000 16210 VQ5 hard copy follows
I would get:
1046338 2018-04-24 07:01:57.000 16210 VQ5 hard copy follows
1064317 2018-03-21 16:21:52.000 16210 VQ5 hard copy follows
1064351 2018-03-21 16:21:47.000 16210 VQ5 hard copy follows
Any help would be greatly appreciated.
edit: I have since found the solution which is as below:
Update t set te_system_ref = ('-' + Convert(nvarchar(50),te_system_ref))
from (
select row_number() over (partition by te_system_ref order by (select 0)) as rn, te_system_ref, te_event from ticket_events where te_system_ref in
(
select te_system_ref from ticket_events where te_event = 'VQ5 hard copy follows' and te_date between '04/12/17' and '18/06/18 23:59:59' group by te_system_ref having count (te_event) > 10
)
and te_event = 'VQ5 hard copy follows'
)t
where t.rn <> 1 and te_system_ref not like '-%'