删除SQL中的类似列

时间:2009-11-02 05:39:18

标签: sql postgresql delete-row

在PostgreSQL 8.3中,假设我有一个名为widgets的表,其中包含以下内容:

 id  | type | count
--------------------
  1  | A    |   21
  2  | A    |   29
  3  | C    |    4
  4  | B    |    1
  5  | C    |    4
  6  | C    |    3
  7  | B    |   14

我想根据type列删除重复项,只留下表中count列值最高的列。最终数据如下所示:

 id  | type | count
--------------------
  2  | A    |   29
  3  | C    |    4  /* `id` for this record might be '5' depending on your query */
  7  | B    |   14

我觉得我很接近,但我似乎无法绕过一个能够摆脱重复列的查询。

3 个答案:

答案 0 :(得分:2)

count是一个sql保留字,因此它必须以某种方式进行转义。我不记得在Postgres中做到这一点的语法,所以我只是用方括号包围它(如果不正确就改变它)。无论如何,以下理论上应该可行(但我实际上没有测试过):

delete from widgets where id not in (
select max(w2.id) from widgets as w2 inner join 
(select max(w1.[count]) as [count], type from widgets as w1 group by w1.type) as sq
on sq.[count]=w2.[count] and sq.type=w2.type group by w2.[count]
);

答案 1 :(得分:1)

有一个比Asaph更简单的答案,使用EXISTS SQL运算符:

DELETE FROM widgets AS a 
WHERE EXISTS 
  (SELECT * FROM widgets AS b 
   WHERE (a.type = b.type AND b.count > a.count) 
          OR (b.id > a.id AND a.type = b.type AND b.count = a.count))

如果以下SQL语句至少返回一条记录,则EXISTS运算符返回TRUE。

答案 2 :(得分:0)

根据您的要求,在我看来这应该有效:

DELETE 
FROM widgets
WHERE type NOT IN 
(
    SELECT type, MAX(count) 
    FROM widgets 
    GROUP BY type
)