Sql 如何在没有唯一标识符的情况下从查询中删除重复行

Sql 如何在没有唯一标识符的情况下从查询中删除重复行,sql,sql-server,duplicates,Sql,Sql Server,Duplicates,我当前正在尝试更新一个包含大量重复行但不包含唯一标识符的表。我想在每个副本的te_system_ref之前添加一个减号,但保留原始。我有下面的查询,它给了我完整的重复列表,如下所示,但我不知道如何只删除重复项 当前查询: select te_system_ref, te_event, te_Date from ticket_events where te_system_ref in (select te_system_ref from ticket_events

我当前正在尝试更新一个包含大量重复行但不包含唯一标识符的表。我想在每个副本的te_system_ref之前添加一个减号,但保留原始。我有下面的查询,它给了我完整的重复列表,如下所示,但我不知道如何只删除重复项

当前查询:

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'
因此,不是:

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
我会得到:

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
任何帮助都将不胜感激

编辑:我找到了如下解决方案:

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 '-%'
你不能

…但不包含唯一标识符

在SQL中,所有具有相同值的行都是等价的。您无法区分看起来相同的两行,因此无法仅更新其中一行


您需要添加某种唯一的键来标识每一行。可能是使用序列、时间戳或其他东西填充的额外列。如果你已经准备好了,那么你就可以开始区分行了,你的生活就会变得轻松多了。

添加一些示例表数据和预期的结果-所有这些都是格式化文本,not images.te_system_ref为rn的分区上的行数,然后选择rn=1或删除其中rn 1用于重复标识为什么您的行数和te_日期介于'04/12/17'和'18/06/18 23:59:59'之间?为什么outer select中缺少此选项?@joop我正在尝试实现此功能,您能告诉我哪里出了问题吗?谢谢您的回复,但我已经找到了答案。最后,我使用row_number创建了所需的唯一标识符。