Case Study #1 - Danny's Diner

July 21, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download Case Study #1 - Danny's Diner...

Description

 

8 Week SQL Challenge #1—Danny’s Diner Thank you Danny Ma for the excellent case study! You can find it  it here  here and try it yourself. I’ve posted the solution and full script on  on  GitHub GitHub  too.

8 Week SQL Challenge  Challenge  

Introduction Danny seriously loves Japanese food so in the beginning of 2021, he decides to embark upon a risky venture and opens up a cute little restaurant that sells his 3 favourite foods: sushi, curry and ramen. Danny’s Diner is in need of your assistance to help the restaurant stay afloat—the restaurant has captured some very basic data from their few months of operation but have no idea how to use their data to help them run the business.

Problem Statement Danny wants to use the data to answer a few simple questions about his customers, especially about their visiting patterns, how much money  they’ve spent and also which menu items are their favourite. Having this deeper connection with his customers will help him deliver a  better and more personalised personal ised experience for his hi s loyal customers. He plans on using these insights to help him decide whether he should expand the existing customer loyalty program—additionally he needs help to generate some basic datasets so his team can easily inspect the data without needing to use SQL. In this challenge, Danny provides information about customers, their orders and diner menu in three different tables ( sales, menu, members).

 

Entity Relationship Diagram

Case Study Questions Each of the following case study questions can be answered using a single SQL statement: 1.  2.  3.  4. 

What is the total amount each customer spent at the restaurant? How many days has each customer visited the restaurant? What was the first item from the menu purchased by each customer? What is the most purchased item on o n the menu and how many times was it purchased by all customers? 5.  Which item was the most popular for each customer? 6.  Which item was purchased first by the customer after they became a member? 7.  Which item was purchased just before the customer became a member? 8.  What is the total items and amount spent for each member before they became a member? 9.  If each $1 spent equates to 10 points and sushi has a 2x points multiplier—how many  points would each customer custo mer have? 10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi—how many points do customer A and B have at the end of January?

Solution Creating Tables  Danny provides his data for PostgreSQL. I’m working on it in SQL Server, so I e dited the schema query for SQL Server. CREATE TABLE sales ( "customer_id" VARCHAR(1), "order_date" "order_date " DATE,

 

"product_id" INTEGER ); INSERT INTO sales ("customer_id", "order_date", "product_id") VALUES ('A', '2021-01-01', '1'), ('A', '2021-01-01', '2'), ('A', '2021-01-07', '2'), ('A', '2021-01-10', '3'), ('A', ('A', '2021-01-11', '2021-01-11', '3'), '3'), ('B', '2021-01-01', '2'), ('B', '2021-01-02', '2'), ('B', '2021-01-04', '1'), ('B', '2021-01-11', '1'), ('B', '2021-01-16', '3'), ('B', '2021-02-01', '3'), ('C', '2021-01-01', '3'), ('C', '2021-01-01', '3'), ('C', '2021-01-07', '3'); CREATE TABLE menu ( "product_id" "product_id " INTEGER, "product_name" VARCHAR(5), "price" INTEGER ); INSERT INTO menu ("product_id", "product_name", "price") VALUES ('1', 'sushi', '10'), ('2', 'curry', '15'), ('3', 'ramen', '12'); CREATE TABLE members ( "customer_id" VARCHAR(1), "join_date" DATE ); INSERT INTO members ("customer_id", "join_date") VALUES ('A', '2021-01-07'), ('B', '2021-01-09');

1.  What is the total amount each customer spent at the restaurant?  Use the  sum function with  group by to find the total amount spent by each customer and the  join function for customer_id (sales table) and price (menu table). select s.customer_id, s.customer_id, sum(m.price) sum(m.price ) amount from sales s left join menu m on s.product_id = m.product_id group by s.customer_id;

From the output, we know that customer A spent $76, customer B spent $74 and customer C spent $36.

 

2.   How many days days has each ccustomer ustomer visited the restaurant? Use the  count function with  group by  to find the number of days visited by each customer. Don’t forget to use  distinct after count   because because there are customers who order more than one  product on the same day. select customer_id, customer_id, count(distinct order_date) days from sales group by customer_id;

From the output, we know that customer A visited 4 days, customer B visited 6 days and customer C visited 2 days. 3.  What was the first item from the menu purchased by each customer?  Use the  rank function with  over partition by to find out the order of each customer. Don’t forget to use where and  group by  because we only want to know the first order of each customer. select customer_id, customer_id, product_name from (select s.customer_id, s.customer_id, s.order_date, m.product_name, dense_rank() over (partition by s.customer_id order by s.order_date) rank from sales s left join menu m on s.product_id = m.product_id left join members ms on s.customer_id s.customer_id = ms.customer_id) a where rank = 1 group by customer_id, product_name;

From the output, we know that the first menu ordered by customer A is curry and sushi, by customer B is curry and by customer C is ramen. 4.  What is the most purchased item on the menu and how many times was it purchased by  all customers?  Use the count function with group by to find how many times an item purchased and the  join function for customer_id (sales table) and product_name (menu table).

 

select top 1 m.product_name, count(s.customer_id) times from sales s left join menu m on s.product_id = m.product_id group by m.product_name order by times desc;

From the output, we know that ramen purchased 8 times is the most purchased item. 5.  Which item was the most popular for each customer?  Use the  count function with  group by  to find how many times an item purchased by each customer, the  rank function with  over partition by to find out the order of popular items by each customer and the  join function for customer_id (sales table) and  product_name (menu table). Don’t forget to use where  because we only want to know the first order of each customer. select customer_id, customer_id, product_name from (select s.customer_id, s.customer_id, m.product_name, count(m.product_name) order_count, dense_rank() over (partition by s.customer_id order by count(m.product_name) desc) rank from sales s left join menu m on s.product_id = m.product_id group by s.customer_id, m.product_name) a where rank = 1;

From the output, we know customers A and C ramen is their favourite, while customer B favourite all menus. 6.  Which item was purchased first by the customer after they became a member?  Use where and  group by to filter days after became a member and the  join function because the required columns are in different tables. select top 1 with ties s.customer_id, m.product_name from sales s left join menu m on s.product_id = m.product_id left members ms s.customer_id = ms.customer_id where join s.order_date > on ms.join_date

 

order by row_number() over (partition by s.customer_id order by s.order_date);

After becoming a member, the first item that customer A ordered ramen while customer B ordered sushi. 7.  Which item was purchased just before the customer became a member?   Use the rank function with over partition by to find out the order of each customer and the join function because the required columns are in different tables. Don’t forget to use where because we only want to know the last order before the customer became a member . select customer_id, customer_id, product_name from (select s.customer_id, s.customer_id, s.order_date, m.product_name, dense_rank() over (partition by s.customer_id order by s.order_date desc) rank from sales s left join menu m on s.product_id = m.product_id left join members ms on s.customer_id = ms.customer_id where s.order_date < ms.join_date) a where rank = 1 group by customer_id, product_name;

Before becoming a member, the last item that customer A ordered curry and sushi while customer B ordered sushi.

 

8.  member? What is the   total items and amount spent for each member before they became a Use the  count and  sum functions with  group by to find the total items and amount spent by each customer and the join function because the requir ed ed columns are in different tables. Don’t forget to use where to filter days before the customer became a member . select s.customer_id, s.customer_id, count(distinct count(disti nct s.product_id) s.product_id) items, sum(m.price) sum(m.price ) amount from sales s left join menu m on s.product_id = m.product_id left join members ms on s.customer_id = ms.customer_id where s.order_date < ms.join_date group by s.customer_id;

 

  Before becoming a member, customer A spent $25 on 2 items while customer B spent $40 on 2 items. 9.   If each $1 spent equates to 10 points and sushi has a 2x points multiplier—how many  points would each each customer h have? ave?  Use case function to create a points column with a note when ordering sushi points multiplied  by 2 and the sum function with group by to find the total points would by each customer. select s.customer_id, s.customer_id, sum(cp.points) sum(cp.poin ts) points from sales s left join (select *, case when product_name = 'sushi' then price*20 else price*10 end points from menu) cp on s.product_id = cp.product_id group by s.customer_id;

From the output, we know that customer A have 860 points, customer B have 940 points and customer C have 360 points. 10. In   In the first week after a custome customerr joins the progr program am (including their join date) they earn 2x points on all items, not just sushi—how many points do custome customerr A and B  have at the end end of January?  Use case function to create a points column with a note when ordering sushi or in the first week after a customer joins the program points multiplied by 2, the join function because the required columns are in different tables and the  sum  function with  group by  to find the total points would by each customer. Don’t forget to use where to filter days because we only want orders in January. select customer_id, customer_id, sum(points) points from (select s.customer_id, s.customer_id, case when product_name = 'sushi' and s.order_date between dateadd(day,-1,ms.join_date) and dateadd(day, 6, ms.join_date) then m.price*40 when product_name = 'sushi' or s.order_date between dateadd(day,-1,ms.join_date) and dateadd(day, 6, ms.join_date) then m.price*20 else price*10 end points from members ms left join sales s on s.customer_id = ms.customer_id left join menu m on s.product_id = m.product_id

 

  where s.order_date = ms.join_date then ‘Y’  ‘Y’   else ’N’ end member  member  from sales s left join menu m on s.product_id = m.product_id left join members ms on s.customer_id = ms.customer_id order by s.customer_id, s.order_date, m.product_name;

 

Rank All The Things

Danny also requires further information about the ranking  of customer products, but he  purposely does not need the ranking for non-member purchases so he expects null ranking  values for the records when customers are not yet part of the loyalty program. Recreate table with column: customer_id, order_date, product_name, price, member(Y/N), ranking(null/number)  select *, case when member = 'Y' then rank() over (partition by customer_id, member order by order_date) else NULL end ranking from (select s.customer_id, s.customer_id, s.order_date, m.product_name, m.price, case when s.order_date >= ms.join_date then 'Y' else 'N' end member from sales s left join menu m on s.product_id = m.product_id left join members ms on s.customer_id s.customer_id = ms.customer_id) a order by customer_id, order_date, product_name;

Insight From the cases that have been analyzed, we get some insights:    

• •

The most popular menu is ramen. Customers A and B are loyal customers and they become members after ordering sushi. Offer sushi to customer C to become a member.

Thank you!

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF