Get the lowest price of a product.
To illustrate my problem:
Row 1
Product_Id
= 1Product_Name
= "iPhone 5"Market_Name
= "Walmart"Product_Original_Price
= "359.00"Product_Promotional_Price
= "319.00"Product_State
= 1 (is on offer)Row 2
Product_Id
= 1Product_Name
= "iPhone 5"Market_Name
= "Apple"Product_Original_Price
= "359.00"Product_Promotional_Price
= "0.00"Product_State
= 0 (isn't on offer)Row 3
Product_Id
= 1Product_Name
= "iPhone 5"Market_Name
= "BestBuy"Product_Original_Price
= "359.00"Product_Promotional_Price
= "299.00"Product_State
= 1 (is on offer)
The query of the next topic (What I have) is returning me zero as the best price of the problem illustrated above — but the best price is 299.00
, by BestBuy
, because zero at Product_Promotional_Price
means that the product isn't on offer.
SELECT
MIN(LEAST(`Product_Original_Price`, `Product_Promotional_Price`)) as `minProductPrice`
[...]
My query:
SELECT `pr`.`Product_Id` as `productId`,
`pr`.`Product_Name` as `productName`,
ROUND(CAST(MIN(`map`.`Product_Original_Price`) AS DECIMAL)/100,2)
as `minProductPrice`,
`prm`.`Product_Measure_Name` as `measureName`,
`prm`.`Product_Measure_Shortname` as `measureShortName`,
`pri`.`Product_Thumbnail_Image_Url` as `thumbnailUrl`,
`pr`.`Product_Markets_Quantity` as `numberOfMarketsThatHaveThisProduct`
FROM `bm_market_products` as `map`
JOIN `bm_products` as `pr` ON `map`.`Product_Id` = `pr`.`Product_Id`
JOIN `bm_products_category_relationship` as `car` ON `pr`.`Product_Id` =
`car`.`Product_Id`
JOIN `bm_product_categories` as `ca` ON `car`.`Category_Id` = `ca`.`Category_Id`
JOIN `bm_products_measure_relationship` as `prmr` ON `pr`.`Product_Id` =
`prmr`.`Product_Id`
JOIN `bm_product_measures` as `prm` ON `prmr`.`Measure_Id` =
`prm`.`Product_Measure_Id`
JOIN `bm_products_images` as `pri` ON `pr`.`Product_Id` = `pri`.`Product_Id`
WHERE ("" IS NULL OR `map`.`Product_State` = 0)
AND ("" IS NULL OR `ca`.`Category_Id` = 14)
GROUP BY `map`.`Product_Id`;
What the query returns:
Considering that Product_State
determines whether a product is on offer or not, follow this fragment:
SELECT `pr`.`Product_Id` as `productId`,
`pr`.`Product_Name` as `productName`,
(IF(`map`.`Product_State` <> 0) THEN
MIN(LEAST(`Product_Original_Price`, `Product_Promotional_Price`))
ELSE (`map`.Product_Original_Price) as `minProductPrice`,
`prm`.`Product_Measure_Name` as `measureName`,
`prm`.`Product_Measure_Shortname` as `measureShortName`,
`pri`.`Product_Thumbnail_Image_Url` as `thumbnailUrl`,
`pr`.`Product_Markets_Quantity` as `numberOfMarketsThatHaveThisProduct`
[...]
Can you see the IF/THEN/ELSE? This is what has been added in relation to the previous query.
The above query doesn't work — syntax isn't correct, I know, but it was just to illustrate.
Gordon Linoff posted this answer and with it, I made this:
SELECT [...]
ROUND(CAST(MIN(CASE WHEN `map`.`Product_Promotional_Price` = 0 THEN `map`.`Product_Original_Price`
ELSE LEAST(`map`.`Product_Promotional_Price`, `map`.`Product_Original_Price`)
end) AS DECIMAL)/100,2) as `minProductPrice`,
[...]
To clarify, I just adapted his [Gordon Linoff] syntax to my scenario — with ROUND
to rounding numbers and CAST
to set a value as a certain type.
Worked perfectly!! Thanks!!
Promotional pricing is a method of generating customer interest in specific items or services which individuals or businesses provide to the public. How this is done, is through a promotional price tag, whereby a consumer is provided full access to a product or service at a price that is lower than its usual price.
The most common promotional pricing types include BOGOF (buy one get one free), seasonal sales promotions, discounts, and flash sales.
There are 4 Pricing Methods that can help you put a price on what you sell: replacement cost, market comparison, discounted cash flow/net present value, and value comparison.
You need to fix your logic for getting the lowest price. A case statement is the best way. Here is an example:
select MIN(case when `Product_Promotional_Price` = 0 then `Product_Original_Price`
else least(`Product_Promotional_Price`, `Product_Original_Price`)
end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With