Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL If Else Statement calculate sum total from 3 tables

Novice here so please go easy!

I want to add a markup price to a products (costprice + supplier delivery charge) which will determine the sell price.
The markup will vary dependent on price range, a category or brand surcharge can be added instead of the markup range price

Distributor Table

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DistyID DeliveryCharge
1 10.00
2 20.00
3 8.00
4 5.00

Stock Table
StockID    |    DistributorID    |    Brand        |    Category    |    SKU        |    CostPrice    |    SellPrice    
——————————————————————————————————————————-
1               |          1                  |    Apple        |    Phone        |    ABC12         |    25.00      |        
2               |          1                  |    Nokia         |    Phone        |    ABC34        |    119.00    |    
3               |          2                  |    Samsung  |    Tablet         |    ABC35        |    242.00    |
4               |          3                  |    Philips       |    TV              |    ABC56        |    333.50    |

Markup Table
ID | Brand   |  Category | DefaultMarkup | MarkupMinPrice | MarkupMaxPrice |MarkupPrice    
————————————————————————————————————————————–
1  |               |   Tablet     |          20             |         0.00              |        9999.99        |     30.00
2  |   Nokia  |                   |          20            |         100.00          |        599.99          |      20.00
3  |               |                   |          20            |          0.00              |       199.99           |      10.00
4  |               |                   |          20            |          200.00         |       299.99           |      15.00
5  |               |                   |          20            |          300.00         |       399.99           |      20.00    

The following will calculate the Stock.SellPrice by adding the Distributor.DeliveryCharge to the Stock.CostPrice

$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SET s.SellPrice = s.CostPrice + d.DeliveryCharge;";

However i need to add the MarkupPrice from the Markup table too.
There may be a surcharge to brand or Category
The markup price will be in a range (Min / Max)

Example

  1. Product costprice is 125.00 from DistyID 3 (no brand / category markup),
    it needs to calculate the sum (125.00 costprice +8.00 deliverycharge)+20.00Markup = SellPrice 153.00
  2. Product costprice is 452.00 from DistyID 4 (brand Nokia), it needs to calculate (452.00 costprice+5.00 deliverycharge)+20.00NokiaMarkup = 477.00 Sellprice

    Markup prices will be based on priority from the markup table rules
    I’m also unsure how to join the Markup table to the Stock table as theres no relationship

I believe something along the lines below is what i’m looking for if anyone can help?

If  (Stock.CostPrice <= Markup.MarkupMinPrice) && (s.CostPrice <= MarkupMaxPrice) {
// BRAND MARKUP
If {
Stock.Brand = Markup.Brand 
$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
}
elseif{
// CATEGORY MARKUP
Stock.Category = Markup.Category
$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
}
else{
// STANDARD PRICE MARKUP 
$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
}
else{
//USE MARKUP.DEFAULT MARKUP 
$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
}

endif;
}

>Solution :

Use multiple LEFT JOIN with the Markup table, matching different columns. Then use COALESCE() to get the markup amounts in priority order.

UPDATE Stock AS s
JOIN Distributor AS d ON s.DistributorID = d.DistyID
LEFT JOIN Markup AS mb ON s.Brand = mb.Brand
LEFT JOIN Markup AS mc ON s.Category = mc.Category
LEFT JOIN Markup AS mp ON s.CostPrice BETWEEN mp.MarkupMinPrice and mp.MarkupMaxPrice
SET s.SellPrice = s.CostPrice + d.DeliveryCharge + COALESCE(mb.MarkupPrice, mc.MarkupPrice, mp.MarkupPrice, 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading