I'm trying to find the 95th percentile (and the highest buy) of the item-price using the order in my ~300k row table.
I've been successful in finding the 95th percentile and the highest buy for one single item with this code:
SELECT type_id,
Max(price) AS buy,
Min(price) AS '95th% buy'
FROM (SELECT *,
( Row_number()
OVER (
partition BY type_id
ORDER BY price DESC) ) AS rownr
FROM orderbuffertest AS rownr
WHERE is_buy_order = 1
ORDER BY ( Row_number()
OVER (
partition BY type_id
ORDER BY price DESC) ) ASC) AS t1
WHERE t1.type_id = 44992
AND t1.rownr < (SELECT Count(*)
FROM orderbuffertest
WHERE is_buy_order = 1
AND type_id = 44992) * 0.05;
However, now I'm trying to GROUP BY type_id
and it's messing up all my values.
Does anybody have an idea of how to GROUP BY type_id
this query? Maybe even ways to improve the original one?
I thank you in advance,
TheJozzle
Ps. Here's a link to my database, if you'd like to mess/test around with it: https://gofile.io/?c=Ga6ODr