Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following database schema for a mobile phone store: To prepare for the New Year Sale, the store wants to make personalized offers to
Consider the following database schema for a mobile phone store: To prepare for the New Year Sale, the store wants to make personalized offers to their customers, The store has decided that customers will get a chance to buy their most expensive purchase again, but this time for a discounted rate. - If the customer previously spent at least $2000 in total, they will get a 90% discount for this sale. - Otherwise, if the customer previously spent at least $1500 in total, they will get a 75% discount for this sale. - Otherwise, if the customer previously spent at least $1000 in total, they will get a 50% discount for this sale. - Finally, if the customer previously spent less than $1000 in total, then we don't include them in the result (no discount for them for this sale). Note that the discount will be applied on the average price of the purchases of the product. For example, if a customer previously bought an iPhone 13 for $1500 and a Samsung Galaxy $22 for $1200 in this store, then the customer will now be eligible for a 90% discount on the average price of iPhone 13 (the discount will be applied to the average price of the product, instead of their previous purchasing price of $1500). Therefore, if the average price of the purchases of an iPhone 13 is $900, then the store's New Year Sale offer to this customer would be: eligible to purchase an iPhone 13 for $90 (\$900 with a 90% discount. They have the 90% discount because they previously purchased $2700 in total, which is more than $2000 ). Another customer might have purchased Samsung Galaxy 522 as their most expensive product, and they'll get a discount on that (the average price of Samsung Galaxy 522) instead. Write a stored procedure with the name Result() that, for every eligible customer, computes the following columns: - Custonerid - offerProductid: the ID of the most expensive product purchased by this customer - Discountrate: 90,75 or 50 depending upon the above rules - Pricedifference: Computed as FLooR(DiscountRate/100 * AVG(all prices of the offered product)). Structure your stored procedure as follows: - Using a cursor, write a query that retums every customer's ID (Custoner Id), and their total purchase value Tota1purchasevalue. - Create a new table named finalTable that will store the output of the stored procedure (see below). - Iterate over the cursor and for every row you fetch, use the totalpurchasevalue to compute the DiscountRate, and the CustomerId to fetch their most expensive purchase (offerproductId). If there are multiple products with same purchase price, pick the product with the larger purchaseId. - Compute the Pricedifference by applying DiscountRate on the average price of the offerproductid. - Insert the custonerId, offerproductid, DiscountRate and the Pricedifference into Finaltable. - After looping over all the rows of the cursor, close the cursor. Then from within the stored procedure, display all rows and columns from Finaltable. Order the results in ascending order by CustonerId. Note: - Do not use DELIMITER and do not call the stored procedure. - Use the exact names that were mentioned, for the stored procedure and new table. - Remember that local variables should not share the same name as attributes/columns (case insensitive). - MySQL wants you to type elseif and not elSE IF! - Do not use views
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started