删除数据总和较少的行

时间:2015-04-01 10:20:21

标签: postgresql

我有下表:

 data_id    sum_value
    xxx        30
    xxx        40
    ccc        50
    aaa        60
    ccc        70
    aaa        80
    ddd        100
    eee        200

如何删除 data_id = data_id sum < sum 的行?如果data_id = data_id和sum_value较小,则删除行,如果data_id!= data_id则显示实际值

预期输出

data_id   sum_value
xxx      40
ccc      70
aaa      80
ddd      100
eee      200

谢谢

5 个答案:

答案 0 :(得分:0)

试试这个:

 delete from table where data_id=data_id and (SELECT min(the_field) FROM the_table)

 select * from table 

答案 1 :(得分:0)

假设你只想保留最大总和(每个id)的记录,并删除其余的记录:

-- the data
CREATE TABLE ztable
        ( data_id CHAR(3) NOT NULL
        , sum_value INTEGER NOT NULL
        );

INSERT INTO ztable(data_id, sum_value) VALUES
 ('xxx',30)
,('xxx',40)
,('ccc',50)
,('aaa',60)
,('ccc',70)
,('aaa',80)
        ;

-- Delete the non-largest per id:
DELETE FROM ztable del            -- del cannot be the record with the largest sum
WHERE EXISTS (                    -- if a record exists
  SELECT * FROM ztable x
  WHERE x.data_id = del.data_id   -- with the same ID
  AND x.sum_value > del.sum_value -- ... but a larger sum
  );

结果:

CREATE TABLE
INSERT 0 6
DELETE 3
 data_id | sum_value 
---------+-----------
 xxx     |        40
 ccc     |        70
 aaa     |        80
(3 rows)

答案 2 :(得分:0)

delete from table
USING table, table as vtable
WHERE (NOT table.ID=vtable.ID)
OR sum=(SELECT min(sum) FROM table)

试试这个

答案 3 :(得分:0)

你可以这样做:

with src as (
    SELECT DISTINCT on (data_id)
       data_id as di, sum_value as sv
    FROM Table1
    ORDER BY data_id, sum_value DESC
 )
 DELETE FROM table1 
 WHERE ( data_id, sum_value) NOT IN (SELECT di, sv FROM src);

保留最高sum_value 的行,或者这样:

with src as (
    SELECT DISTINCT on (data_id)
       data_id as di, sum_value as sv
    FROM Table1
    ORDER BY data_id, sum_value ASC
 )
 DELETE FROM table1 
 WHERE ( data_id, sum_value) IN (SELECT di, sv FROM src);

删除最小sum_value 的行。

对于提供的数据,两者都会这样做。你没有提到你期望在同一个data_id存在两行以上的内容。

看小提琴:http://sqlfiddle.com/#!15/92a04/1

当您在右窗格中修改数据时,Hmm sqlFiddle表现很奇怪。当它们运行时,它都显示0行受到第一行的影响,但是第二行仅显示3行。但是,如果单独运行它们 - 它表示没有删除任何行,并且所有行仍然存在。我猜它会在每次运行后返回左窗格中定义的状态的表...它必须在事务中运行它。当我添加COMMIT;语句时,它引发了错误:

  

查询面板中不允许显式提交。

在您的本地数据库上尝试theese查询 - 这就是它在我的本地postgres 9.4上的外观:

testdb=# select * from Table1;
 data_id | sum_value
---------+-----------
 xxx     |        30
 xxx     |        40
 ccc     |        50
 aaa     |        60
 ccc     |        70
 aaa     |        80
(6 wierszy)


testdb=# with src as (
testdb(#     SELECT DISTINCT on (data_id)
testdb(#        data_id as di, sum_value as sv
testdb(#     FROM Table1
testdb(#     ORDER BY data_id, sum_value ASC
testdb(#  )
testdb-#  DELETE FROM table1
testdb-#  WHERE ( data_id, sum_value)  IN (SELECT di, sv FROM src);
DELETE 3
testdb=# select * from Table1;
 data_id | sum_value
---------+-----------
 xxx     |        40
 ccc     |        70
 aaa     |        80
(3 wiersze)

重新初始化表

testdb=# select * from Table1;
 data_id | sum_value
---------+-----------
 xxx     |        30
 xxx     |        40
 ccc     |        50
 aaa     |        60
 ccc     |        70
 aaa     |        80
 ddd     |       100
 eee     |       200
(8 wierszy)


testdb=# with src as (
testdb(#     SELECT DISTINCT on (data_id)
testdb(#        data_id as di, sum_value as sv
testdb(#     FROM Table1
testdb(#     ORDER BY data_id, sum_value DESC
testdb(#  )
testdb-#  DELETE FROM table1
testdb-#  WHERE ( data_id, sum_value) NOT IN (SELECT di, sv FROM src);
DELETE 3
testdb=#  SELECT * from table1;
 data_id | sum_value
---------+-----------
 xxx     |        40
 ccc     |        70
 aaa     |        80
 ddd     |       100
 eee     |       200
(5 wierszy)

适合我...

答案 4 :(得分:0)

delete from foo 
using(select min(sum_value) sum
            ,data_id 
     from foo 
     group by data_id  
     having count(data_id)>1
     )t
where foo.sum_value=t.sum 
      and foo.data_id=t.data_id  

SqlFiddle - Demo