How do I find the largest factor of an integer in mysql

Posted by Bill H on Stack Overflow See other posts from Stack Overflow or by Bill H
Published on 2010-05-03T18:10:55Z Indexed on 2010/05/03 18:18 UTC
Read the original article Hit count: 270

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about factorial