如果我有几对相同的行,如何删除第一行

时间:2013-08-29 15:22:02

标签: database sqlite

我有一个名为'SULP'的sqlite表,看起来像这样:

name year month day hour min ro   
"SULP","12","7","7","0","0","2.41196"   *
"SULP","12","7","7","0","0","2.39269"
"SULP","12","7","7","0","1","2.41117"   *
"SULP","12","7","7","0","1","2.39198"
"SULP","12","7","7","0","2","2.41004"   *
"SULP","12","7","7","0","2","2.39117"
"SULP","12","7","7","0","3","2.40914"   *
"SULP","12","7","7","0","3","2.39043"

我不会删除标有*

的行

我试过了

delete from SULP 
       where exists (select * from 'SULP' as t2 WHERE t2.year=year
       AND t2.month=month AND t2.day=day AND t2.hour=hour AND 
       t2.min=min and ro<>t2.ro order by rowid desc limit 1);

delete from 'SULP' 
      where exists (select * from 'SULP' as t2 WHERE t2.year=year 
      AND t2.month=month AND t2.day=day AND t2.hour=hour AND 
      t2.min=min AND rowid<t2.rowid);

但没有运气:(

3 个答案:

答案 0 :(得分:1)

这将做你想要的:

DELETE FROM SULP
WHERE EXISTS (
SELECT *
FROM SULP t2
GROUP BY name, year, month, hour, min
HAVING SULP.rowid = min(t2.rowid)  
)

参见 SQLFIDDLE

答案 1 :(得分:1)

另一种解决方案:

DELETE FROM SULP WHERE (
    SELECT MIN(ro) FROM SULP AS t2
    WHERE year  = SULP.year
      AND month = SULP.month
      AND day   = SULP.day
      AND hour  = SULP.hour
      AND min   = SULP.min
   ) <> ro;

答案 2 :(得分:0)

在子查询中,t2.yearyear都引用t2表的列。

您必须使用SULP.year来引用外部表格:

DELETE FROM "SULP"
WHERE EXISTS (SELECT 1
              FROM "SULP" AS t2
              WHERE t2.year  = SULP.year
                AND t2.month = SULP.month
                AND t2.day   = SULP.day
                AND t2.hour  = SULP.hour
                AND t2.min   = SULP.min
                AND t2.ro    < SULP.ro)