I have this query for a shipping calculator that is supposed to select out the lowest value from a database based on several criteria such as weight and country. The query is meant to get the first lowest value for one or multiple ShippingProviderIds that match the criteria.
The problem is the group by query mixes the results of several rows from the ShippingCost table and throws hings out of whack. I need Price and ShippingCostId to be from the same row. How can I prevent this mixing of results? Maybe Group By is not what I need?
SELECT MIN(s1.Price), s1.ShippingCostId, s1.Weight, s2.CoD, s2.Artnr, s2.CoD,
s2.Regular, s2.ShippingProviderId, s3.ProviderName FROM ShippingCost s1
INNER JOIN ShippingProvider s2 ON s1.ShippingProviderId = s2.ShippingProviderId
INNER JOIN ShippingProviderTranslations s3 ON (s2.ShippingProviderId = s3.ShippingProviderId AND s3.Language = 'xx')
WHERE CountryId = 123 AND (Weight >= 0.5 AND Weight <= 50) AND s2.Regular = 0
GROUP BY s2.ShippingProviderId
If I remove the MIN() expression the rows do no not mix anymore, but the lowest value is not the one found, because the lowest value it has a higher ShippingCostId and was added later (is primary key). It does not work to add the MIN() expression for weight either, or to try and sort by weight. I've tried a whole number of different approaches but I am feeling stumped.
Any suggestions?