0

Example,

    SELECT * FROM stats_table
order by accuracy desc, totals desc;

This is ordered by accuracy desc, totals desc

and it returns the following

id  col_a       amount  col_b       accuracy   totals
881 stat_a      38.0    stat_b      71.60       162 
884 stat_a      39.0    stat_b      70.52       173 
109 stat_a      38.0    stat_b      69.91       105 
880 stat_a      38.0    stat_b      69.88       249 
883 stat_a      39.0    stat_b      69.47       262 

I want the row removed if the accuracy AND totals is both lower than the row above, so from the example, id 109 should be removed as accuracy and totals are both lower than the row above, id 884

So it should end up like this

id  col_a       amount  col_b       accuracy   totals
881 stat_a      38.0    stat_b      71.60       162 
884 stat_a      39.0    stat_b      70.52       173 
880 stat_a      38.0    stat_b      69.88       249 
883 stat_a      39.0    stat_b      69.47       262 

Ideally, as there are 1000s of rows, it should loop through the whole table.

please help

1 Answers1

0

To do it in the way you have described you could assign a row number:

select *
from stats_table
where id in (
  select a.id
  from (SELECT *, row_number()over(order by accuracy desc, totals desc) as row FROM stats_table) a
  inner join (SELECT *, row_number()over(order by accuracy desc, totals desc) as row FROM stats_table) b
  on a.row = b.row + 1
  and a.accuracy < b.accuracy
  and a.total < b.total
)

Then you just need to replace 'select *' with 'delete from' when happy that the rows you want deleted have been selected (I'm unable to test the code for obvious reasons).

Vorpulus Lyphane
  • 660
  • 6
  • 19