我正在尝试使用以下代码删除重复项:
delete [TABLE] f
where id not in (select min(id)
from [TABLE] s
where f.companyid = s.companyid
and f.value = s.value)
但是,它给出了以下错误消息:
Msg 102,Level 15,State 1,Line 1
'f'附近的语法不正确。
语法有什么问题?
答案 0 :(得分:1)
在SQL Server中,我喜欢使用CTE和row_number()
来实现此目的:
with todelete as (
select t.*,
row_number() over (partition by companyid, value order by id) as seqnum
from t
)
delete todelete
where seqnum > 1;
但你的语法问题是它应该开始:
delete f from [TABLE] f
where . . .
换句话说,表别名在FROM
子句中定义。
答案 1 :(得分:0)
delete from [TABLE] f
where id not in (select min(id) from [TABLE] s
where f.companyid = s.companyid
and f.value = s.value)
答案 2 :(得分:0)
DELETE FROM [TABLE] f
WHERE id NOT IN ( SELECT MIN(id) FROM [TABLE] s
where f.companyid = s.companyid and f.value = s.value)
答案 3 :(得分:0)
从f中删除id不在(从f中选择min(id),其中f.companyid = s.companyid和f.value = s.value);
尝试。
答案 4 :(得分:0)
它给出错误,因为表的别名使用错误。要使用删除语句对表进行别名,您可以使用;
delete f from [TABLE] as f
where id not in (
select min(id) from [TABLE] s
where f.companyid = s.companyid
and f.value = s.value
)