I have something like this:
Name.....Value
A...........10
B............9
C............8
Meaning, the values are in descending order. I need to create a new table that will contain the values that make up 60% of the total values. So, this could be a pseudocode:
set Total = sum(value)
set counter = 0
foreach line from table OriginalTable do:
counter = counter + value
if counter > 0.6*Total then break
else insert line into FinalTable
end
As you can see, I’m parsing the sql lines here. I know this can be done using handlers, but I can’t get it to work. So, any solution using handlers or something else creative will be great.
It should also be in a reasonable time complexity – the solution how to select values that sum up to 60% of the total
works, but it’s slow as hell 🙁
Thanks!!!!
You’ll likely need to use the
lead()orlag()window function, possibly with a recursive query to merge the rows together. See this related question:merge DATE-rows if episodes are in direct succession or overlapping
And in case you’re using MySQL, you can work around the lack of window functions by using something like this:
Mysql query problem