I am currently trying to optimize a few queries and scripts, and I wonder if there is a way to cut off a certain percentage of a MySQL Result Set?
for example, I have a Query like this:
SELECT TrackingID, OpenTime, FirstPoint FROM PointsTable
I need the ‘middle’ 74%, ordered by the difference between FirstPointGMT and OpenTimeGMT, which means: I need to cut off the top and bottom 13%.
At the moment, a Script will just get all Rows, calculate and order by duration and then cut them off, but I’d like to do it in an SQL Query if possible to make the script clearer.
I know about the LIMIT Clause, but that does not seem to support relative values like ’13 percent’.
Can anyone tell if a) if that is actually possible only with SQL and b) how it could be achieved?
MySQL is 5.0.45.
PS: And yes, I know that 13% seems like a very strange number, but that’s all part of a bigger process.
You need to LIMIT the data you are returning by what percentile the returned rows in the resultset are at.
Try this:
Explanation of the above
The only tricky thing here is the Limit clause, and its syntax:
Our start_row needs to be the first element after the first record 13/100ths (13% of the way) into the result set. To do that, we use this formula:
which is
(13*((SELECT COUNT(*) FROM PointsTable) +1) / 100).Our number_of_rows_to_return has to be the total number of rows, minus 26% of the rows (13% from the bottom and 13% from the top), or
Thus the actual calculation is:
EDIT: After looking over Niyaz’s response, I realized that the query could be rewritten as:
This is cleaner, as it eliminates a third nested query, and should be functionally equivalent. Limit rows, start at the rows 13% from top, and show 74% of the total rows out of the table from that point on.