I have an inventory table that lists quantities at multiple locations. I keep a quantity value and I now have to keep a "threshold" value, and when I reach the threshold stop selling the product. It's just a fudge factor so when people have inaccurate inventory there is a little buffer to make sure they don't over commit sales.
SELECT product, sum(quantity) as pooled, max(quantity) as maxincart FROM `inventory` WHERE location=1 OR location=2 OR location=3 GROUP by product
The main problem with the queries I have tried is that they sum all of my locations. That was fine before thresholds. Now locations that I'd rather ignore are included in my sums, I would like an IF to remove them from the aggregate functions.
Sometimes it works fine.
locationA has 7 and a threshold of 3.
locationB has 9 and a threshold of 4.
Sum it all up, and it's 16 - 7 = 9 Works fine!
But then there are situations like this
locationA has 7 and a threshold of 3.
locationB has 0 and a threshold of 4.
Sum it all up, and it's 7 - 7 = 0. LocationA wants to sell their 4 but can't now.
Is there a way to nest an if inside a query so that when a location's qty-threshold <=0 then it will be excluded from the sum? I'm trying to do it in mysql to keep it quick, found some good info about using in select, but combining it with the group by and aggregate functions has me stuck.
The table I'm querying is an intermediate table and can be restructured. So I'm open to ideas about changing the table to make it work. I just want it to be quick so I don't want to have to do another query inside each product. I've considered eliminating the group by, ordering by product, storing values in an array, and when the product changes running through the array with this logic...but that also means a much larger loop and more memory used. I'm hoping some mysql genius out there can lend a hand and help. Thanks!
Aucun commentaire:
Enregistrer un commentaire