How do I find the largest factor of an integer in mysql
- by Bill H
I am trying to write a select query that will dynamically determine the minimum number of items that can be packaged together. I am having trouble with one part of the query.
...
CASE
WHEN (pid.product_id) THEN 1
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 11 = 0)) THEN CEILING(p.inner_pack / 11)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 7 = 0)) THEN CEILING(p.inner_pack / 7)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 6 = 0)) THEN CEILING(p.inner_pack / 6)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 5 = 0)) THEN CEILING(p.inner_pack / 5)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 4 = 0)) THEN CEILING(p.inner_pack / 4)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 3 = 0)) THEN CEILING(p.inner_pack / 3)
WHEN ((p.case_pack = p.inner_pack) AND (p.inner_pack % 2 = 0)) THEN CEILING(p.inner_pack / 2)
ELSE p.inner_pack
END AS min_pack
...
What I want to do is find the largest factorial of an integer (p.inner_pack) that is under 12. Is there a better way to do this in mysql?