2

可能重复:
SQL - 如何删除重复行?

我有一个结构如下的表:

create table MnA
(id         int PRIMARY KEY IDENTITY    not null,
symbol      nvarchar(4)                 not null,
direction   nvarchar(4)                 not null,
start_dt    nvarchar(5)                 not null,
end_dt      nvarchar(5)                 not null,
start_doy   int                         not null,
end_doy     int                         not null,
avg_price   int                         not null,
min_price   int                         not null,
max_price   int                         not null,
avg_percent int                         not null,
min_percent int                         not null,
max_percent int                         not null,
history     text                        not null,
percent_hit int                         not null,
aw_length   int                         not null,
diff        int                         not null,
date_change int                         not null)

我想删除有相似之处的行。

如果该行相同symbol, direction, start_doydiff那么我想保留最高的那一行avg_percent

我将如何做到这一点?

4

1 回答 1

4
DELETE target 
FROM MnA target INNER JOIN MnA temp 
     ON (target.symbol = temp.symbol 
         AND target.direction = temp.symbol
         AND target.start_doy = temp.start_doy
         AND target.diff = temp.diff
         AND target.id != temp.id 
         AND temp.avg_percent > target.avg_percent);

演示(sqlfiddle)。

于 2012-08-21T22:47:17.113 回答