我的数据行有一个非常大的问题,我应该只有8行,然后我得到了一个SQL查询,它让我(我认为)应该删除的所有行,就像这样...
编辑:还有没有PKEY,这就是复制
的原因select a1.ID, a1.serie, a1.tienda, a1.numtransa, a1.sistema, a1.factura, a1.jfecha, a1.codart from posmov a1
inner join posmov a2
on a1.tienda = a2.tienda
and a1.numtransa = a2.numtransa
and a1.sistema= a2.sistema
and a1.factura =a2.factura
and a1.jfecha = a2.jfecha
and a1.codart = a2.codart
and a1.serie =a2.serie
and a1.ID > a2.ID;
但是当我想删除它们时添加到最后一个查询
delete from posmov
where ID in ( LAST QUERY);
我得..
Msg 116, Level 16, State 1, Line 30
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
如果有更简单的方法,查询或实际删除重复项,有人可以给我注意。
答案 0 :(得分:1)
只需在查询中选择ID,只要您使用IN
就可以列出值('1','2','3'),或者您可以从子查询中选择单个字段,但不能多个字段:
delete from posmov
where ID in (
select a1.ID
inner join posmov a2
on a1.tienda = a2.tienda
and a1.numtransa = a2.numtransa
and a1.sistema= a2.sistema
and a1.factura =a2.factura
and a1.jfecha = a2.jfecha
and a1.codart = a2.codart
and a1.serie =a2.serie
and a1.ID > a2.ID)
或者,您可以使用EXISTS
:
delete from posmov
where EXISTS ( LAST QUERY);