1

我对数据行有一个非常大的问题,我最多可以有 1k 行,而应该只有 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.

如果有更简单的方法,有人可以启发我进行查询或实际删除重复项。

4

1 回答 1

2

只需在查询中选择 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);
于 2013-07-18T22:14:36.390 回答