I have a data table that looks like this:
---reservation_discount-----
id relation_id title rate
1 2 25% Discount 25
2 2 5% Discount 5
---reservation_hotel-----
id room_id price
2 1 2430
My query:
SELECT
reservation.customer,
hotel.title,
reservation_hotel.price,
reservation_discount.rate,
reservation_hotel.price * (100 - sum(reservation_discount.rate)) / 100 as total
FROM
reservation_hotel
INNER JOIN reservation ON reservation_hotel.reservation_id = reservation.id
INNER JOIN hotel ON reservation_hotel.hotel_id = hotel.id
INNER JOIN room ON reservation_hotel.room_id = room.id AND room.hotel_id = hotel.id
INNER JOIN reservation_discount ON reservation_discount.relation_id = reservation_hotel.id
WHERE reservation_hotel.id=1
My question is:
I want him to take the discounts in order, but he doesn't get 25% and 5%, he gets 30%.
How do I apply the discounts in order?
Price: 2430 - 25% = 1822,5 and 1822,5 - 5% = 1731.375
As a more complex algorithm, this can have as many discount as you like to give and you can also select individual discount everytime.
In this example i took both reservation_hotel.id , but you can only use 1 or even all, as you wish. In this select query the reservation_hotel.id is called rh.id
This select statement
SELECT
MIN(customer) customer,
MIN(title) titke,
MIN(price) price ,
MIN(totalrate * 100) rate,
MIN(total) total
FROM
(SELECT
rh.id id,
r.customer customer,
h.title title,
rh.price price,
rd.rate rate,
if (@res = rh.id,@total :=@total,@total :=0) disc,
if (@res = rh.id,@rate :=@rate,@rate :=0) disc2,
if (@rate = 0, @rate := (100 - rd.rate)/ 100,@rate := @rate * (100 - rd.rate)/ 100) totalrate,
if (@total=0,@total := rh.price * (100 - rd.rate) / 100,@total := @total * (100 - rd.rate) / 100) total,
@res := rh.id
FROM
reservation_hotel rh
INNER JOIN reservation r ON rh.reservation_id = r.id
INNER JOIN hotel h ON rh.hotel_id = h.id
INNER JOIN room ro ON rh.room_id = ro.id AND ro.hotel_id = h.id
INNER JOIN reservation_discount rd ON rd.relation_id = rh.id
,(SELECT @res := 0) r1,(SELECT @total := 0) r2,(SELECT @rate := 0) r3
WHERE rh.id=1 or rh.id=2
ORDER BY rh.id ) t1
GROUP BY id
ORDER BY id;
Gives you following result:
customer | titke | price | rate | total :------- | :------------ | ----: | ----: | -------: John Doe | Premium Hotel | 2430 | 71.25 | 1731.375 John Doe | Premium Hotel | 2250 | 71.25 | 1603.125
DBfiddle example https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=276a5474b1329d018ffacdc910ea5f10
It calculates the correct actual rate of the customer and the total amount.
To solve this in a genereric manner, you need some kind of recursion. If you are running MySQL 8.0, you can use a recursive common table expression.
The idea is to initially join the reservation_hotel with the record in reservation_discount that has the smallest id (using a correlated subquery), and to apply that first discount. Then, we get to the next discount, and apply it to the previously calculated price.
This query gives you the step by step calculation:
with recursive cte as (
select r.*, r.price * (100 - d.rate) / 100 final_price, d.id discount_id
from reservation_hotel r
inner join reservation_discount d on d.id = (
select min(d1.id) from reservation_discount d1 where d1.relation_id = r.id
)
union all
select c.id, c.room_id, c.price, c.final_price * (100 - d.rate) / 100, d.id
from cte c
inner join reservation_discount d on d.id = (
select min(d1.id)
from reservation_discount d1
where d1.relation_id = c.id and d1.id > c.discount_id
)
)
select * from cte order by discount_id
id | room_id | price | final_price | discount_id -: | ------: | ----: | ----------: | ----------: 2 | 1 | 2430 | 1822.5000 | 1 2 | 1 | 2430 | 1731.3750 | 2
If you just want the final price, then you can use aggregation:
with recursive cte as (
select r.*, r.price * (100 - d.rate) / 100 final_price, d.id discount_id
from reservation_hotel r
inner join reservation_discount d on d.id = (
select min(d1.id)
from reservation_discount d1
where d1.relation_id = r.id
)
union all
select c.id, c.room_id, c.price, c.final_price * (100 - d.rate) / 100, d.id
from cte c
inner join reservation_discount d on d.id = (
select min(d1.id)
from reservation_discount d1
where d1.relation_id = c.id and d1.id > c.discount_id
)
)
select id, room_id, price, min(final_price) final_price
from cte
group by id, room_id, price
id | room_id | price | final_price -: | ------: | ----: | ----------: 2 | 1 | 2430 | 1731.3750
Demo on DB Fiddle
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