简体   繁体   中英

SQLite delete rows not having max index

I've tried already the search function and the most appropriate result was this, and it seems not to work for my case (?!):

[ sql, keep only max value and delete others

I have one table like this

 parentassembly | "Index" | partumber 
-----------------  ------------ -------------
pA 1| _00| part1 
pa 1| _00| part2
pa 1|_ab| part1
pa 1|_ab| part3
pa 1|_af| part1
pa 1|_af| part2
pa 1|_af| part3
pa 1|_af| part4
pa 2| |part12
pa 2|| part...  
pa 2|....| .....

I need only the parentassembly with the highest Index and the subsequent parts.

The index can be numerical or alphanumerical or empty.

If I try the approach as shown in the link but do a count instead, I do not get a different number as all table rows

select count(*)
 (...or delete r....) 
from pa_save r left join
    (select parentassembly, Max("Index") as maxindex
        from pa_save
        group by parentassembly
        ) rn
        on r.ParentAssembly = rn.ParentAssembly = rn.maxindex
    where rn.ParentAssembly is null;

I already tried also with subqueries, also not archiving a different result...

delete from  pa_save 
where parentassembly not in 
    (select parentassembly from pa_save where "Index" = 
    (select MAX("Index") from pa_save as pa_save2 
          where pa_save2.parentassembly = pa_save.parentassembly));

Besides that it seems not to work, the subquery takes endless....

Any insight would be really appreciated.

Kind regards,

Tobias

Your first query has several syntax anomalies. Does this do what you want?

select r.*
from pa_save r join
     (select parentassembly, Max("Index") as maxindex
      from pa_save
      group by parentassembly
     ) rn
     on r.ParentAssembly = rn.ParentAssembly and
        r."Index" = rn.maxindex;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM