Silverkight Viewer for Reporting Services
Got a SQL Server or .NET question? Discuss it in the forums. (SQL Server Forums | Dot NET Forums)
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

We hope you enjoyed the previous challenge and it is time to look at the next challenge. We are very excited to introduce TSQL Challenge #11, which we think all of you will enjoy solving. Just like we did with the previous challenge, there will be a FREE training webcast for those people who find it hard to solve. If you would like to attend the training webcast, please register at http://tsqlchallenge11.eventbrite.com/.

If you have comments, questions, challenge ideas or problems that you think should be interesting to discuss, don’t hesitate to post them on the dedicated forum threads or write to us.

The Context

The context of this challenge is about dealing with combinations and how to extract valuable data. For this challenge we will work with two data tables.

The products with their prices:

ID NAME    PRICE
-- ------- ---------
1  PROD 1  100,00
2  PROD 2  220,00
3  PROD 3  15,00
4  PROD 4  70,00
5  PROD 5  150,00

And the discount coupons:

ID NAME         VALUE  IS_PERCENT
-- -----------  ------ ----------
1  CP 1 : -15$  15     0
2  CP 2 : -5$   5      0
3  CP 3 : -10%  10     1
4  CP 4 : -12$  12     0

On the IS_PERCENT column, you should understand, if 0 value is considered as real money value, if 1 then it is a percent value of the price.

The Challenge

For this shopping application, customers could use one to two coupons for the same product but the discount price can not be less than 70% of the original price and the total amount of the discount can not exceed 30$.

It is important to note that coupons are applied in a cumulative way. The second coupon is applied on the result of the original price + first coupon.

With these conditions, the boss ask for a report that shows for each product the minimum price that should be paid for a product using any combination of the discount coupons. For this report you need also to show:

  • Original price (PRICE)
  • Discount price (DISC_PRICE)
  • Total amount of the discount (TOT_DISC)
  • Total rate of the discount (RATE)
  • An info field with names of coupons applied (COUPON_NAMES)

Regarding the original data tables, here is the final report you should produce:

ID NAME    PRICE    DISC_PRICE  TOT_DISC  RATE    COUPON_NAMES
-- ------  -------- ----------- --------- ------- -------------------------
1  PROD 1  100.00$  73.00$      27.00$    27.00%  CP 4 : -12$ + CP 1 : -15$
2  PROD 2  220.00$  193.00$     27.00$    12.27%  CP 4 : -12$ + CP 1 : -15$
3  PROD 3  15.00$   13.50$      1.50$     10.00%  CP 3 : -10%
4  PROD 4  70.00$   49.50$      20.50$    29.28%  CP 1 : -15$ + CP 3 : -10%
5  PROD 5  150.00$  120.00$     30.00$    20.00%  CP 3 : -10% + CP 1 : -15$

Sample Data

As usual, you should use exactly the sample data as it is provided in your solution script:

DECLARE @T TABLE (ID INT IDENTITY, NAME NVARCHAR(20),PRICE MONEY)
INSERT INTO @T (NAME,PRICE) VALUES ('PROD 1',100) 
INSERT INTO @T (NAME,PRICE) VALUES ('PROD 2',220) 
INSERT INTO @T (NAME,PRICE) VALUES ('PROD 3',15) 
INSERT INTO @T (NAME,PRICE) VALUES ('PROD 4',70) 
INSERT INTO @T (NAME,PRICE) VALUES ('PROD 5',150) 


DECLARE @C TABLE (ID INT IDENTITY, NAME NVARCHAR(20), VALUE INT, IS_PERCENT BIT) 
INSERT INTO @C (NAME,VALUE,IS_PERCENT) VALUES ('CP 1 : -15$',15,0) 
INSERT INTO @C (NAME,VALUE,IS_PERCENT) VALUES ('CP 2 : -5$',5,0) 
INSERT INTO @C (NAME,VALUE,IS_PERCENT) VALUES ('CP 3 : -10%',10,1) 
INSERT INTO @C (NAME,VALUE,IS_PERCENT) VALUES ('CP 4 : -12$',12,0)

Notes

  1. As usual, write a single query that produces the expected output.
  2. You should target any version of Sql Server
  3. Send your entries to tc@beyondrelational.com
  4. Do not include your solutions in the body of the email. Send them as an attachment in the email. Name it “firstname_lastname.sql”.
  5. Add ‘TSQL Challenge #11’ in the subject line of the email.
  6. Last date to submit your entries: 27 July 2009
  7. Use this forum for any questions related to TSQL Challenge #11

Share

Comments

# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Thursday, July 16, 2009 9:42 AM by Brian Tkatch

[posting here as i'm getting access dnied on the linked forum]

There's a details left out. Percentage discounts are affected by coupon order. That is, if percentage is first it is a discount off the original price. If it is second, it is off the discounted price. This makes a difference for Prod 4 where the total is 70.

price is 70

-10% then -15 = -7 + -15 = -22

-15 then - 10% = -15 + -10% = -15 + -5.50 = -20.50

The total discount cannot be grater than 30%. 30% of 70 is 21. Which means that although the -15 and -10% can be applied, they can only be applied when the -10% is second.


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Thursday, July 16, 2009 10:40 AM by Rui Carvalho

Hi Brian,

I added a explanation about the coupons sequence to be more clear.

To get access to the linked forum you just need to be part of the TSQL Challenges group (which is not the case currently as far as I can see on your profile)


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Thursday, July 16, 2009 2:40 PM by Brian Tkatch

Rui

Thanx. I didn't realize i had to join the group first.


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Thursday, July 16, 2009 6:18 PM by Cleber

Second discount is applyed for the discount price after first coupon, correct?


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Friday, July 17, 2009 4:40 AM by Rui Carvalho

Hi Cleber,

Exactly, the second coupon is applied on the result of original price and first coupon. If you have a price of 100$, and applies a coupon of 10$ and another of 10% you should have final price = (100-10)*0.9


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Sunday, July 19, 2009 2:20 PM by Fabien "Waldar" Contaminard

Hello to the team,

Prod 2 has two solutions : the mentioned CP4 + CP1 and CP3 + CP2.

Is there any way to choose one over the other ?

Maybe you should also mention that a discount coupon can only be used once.

Thanks for the challenge !


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Wednesday, July 22, 2009 7:51 AM by Fabien "Waldar" Contaminard

Oh btw, while writing a news about this challenge, I saw that your link to your free webcast is not leading to tsqlchallenge11.eventbrite.com but to this very same news.


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Wednesday, July 22, 2009 11:30 AM by Rui Carvalho

Thanks you Fabien to point this, it is corrected now


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Sunday, July 26, 2009 11:30 AM by Peso

It doesn't say that multiple use of same coupon is prohibited...

What if I use "Coupon 2" twice for item "Prod 2"?

The discounted price is then 190$ or 13.64%.


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Sunday, July 26, 2009 1:06 PM by Rui Carvalho

hi Peso,

I miss that point. In fact, I just hoped that it should be obvious regarding the explanations and the final result set.

So, to be clear, "Each coupon can be used only one time on each couple of coupons".

Thanks to point that ;-)


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Friday, August 21, 2009 2:33 AM by Hariharan

Can we assume that the coupon list is fixed to 4 or it can be variable?

-Hari


# re: TSQL Challenge 11 – Calculate the lowest price of an Item by applying the best combination of discount coupons

Friday, August 21, 2009 5:10 AM by Rui Carvalho

Hi Hari,

the coupon list is variable and should  be managed as any other data in a table ;-)


Copyright © Beyondrelational.com