mysql - SQL Pricing Tiers -
i have pricing lookup table in mysql need lookup right pricing based on transaction quantity.
say example, have pricing table pricing, looks like:
product quantity price prod1 1 4 prod1 10 3 prod1 100 2 prod1 1000 1 prod2 1 0.4 ... and have table called transaction contains sales data:
product sales prod1 144 prod2 2 ... how can sales multiply right unit price based on quantity. likes:
product sales quantity unitprice prod1 144 100 2 prod2.... i tried join 2 table on product don't know go there, thanks!
one way price using correlated subquery:
select t.*, (select p.price pricing p p.product = t.product , p.quantity >= t.quantity order p.quantity limit 1 ) price transaction t; a similar subquery can used other information such pricing tier.
for performance, want index on pricing(product, quantity).
Comments
Post a Comment