Giter Club home page Giter Club logo

customer-behaviour-analysis-using-mysql's Introduction

Customer-Behaviour-Analysis-Using-MYSQL

This project and the data used was part of a case study which can be found here. It focuses on examining patterns, trends, and factors influencing customer spending in order to gain insights into their preferences, purchasing habits and potential areas for improvement in menu offerings or marketing strategies in a dining establishment.

Background 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 favorite 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 some questions about his customer, especially about their visiting patterns, how much money they’ve spent and also which menu items are their favorite. Having this deep connection with his customers will help him deliver a better and more personalized experience for his 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. Danny has provided you with a sample of his overall customer data due to privacy issues - but he hopes that these examples are enough for you to write fully functioning SQL queries to help him answer his questions!.

Entity Relationship Diagram

Skills Applied • Windows Functions • CTEs • Aggregations • JOINS • Write scripts to generate basic reports that can be run every period Questions Explored

  1. What is the total amount each customer spent at the restaurant?

  2. How many days has each customer visited the restaurant?

  3. What was the first item from the menu purchased by each customer?

  4. What is the most purchased item on 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 first just before the customer became a member?

  8. What is the total items and amount spent for each member before they a member?

  9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier – how many points would each customer have?

  10. In the first week after a customer joins the program (including their join date) they earn 2xpoints on all items, not just sushi – How many points do customer A and B have at the end of January?

Some interesting Queries

With customer_popularity AS ( 
      SELECT s.customer_id, m.product_name, COUNT(*) AS purchase_count,
                      DENSE_RANK() OVER (PARTITION BY  s.customer_id  ORDER BY COUNT(*) DESC) AS rank
     FROM dbo.sales s
    INNER JOIN dbo.menu  m ON  s.product_id  = m.product_id
  GROUP BY s.customer_id,  product_name
)
SELECT customer_id,  product_name,  purchase_count
FROM customer_popularity
WHERE rank = 1;
Q10  - In the first week after a customer joins the program ( including their join date) they earn 2xpoints on all items , not just sushi – how many points do customer do customer A and B have at the end of January?
SELECT s.customer_id, SUM (
      CASE
           WHEN s.order_date BETWEEN  mb.join_date  AND DATEADD(day, 7, mb.join_date) THEN m.price*20
           WHEN m.product_name  = ‘sushi’ THEN m.price*20
           ELSE m.price*10
END) AS total_points
FROM dbo.sales s 
JOIN dbo.menu m ON s.product_id = m.product_id
LEFT JOIN dbo.members mb  ON  s.customer_id  = mb.customer_id
WHERE s.customer_id  IN  (‘A’, ‘B’)  AND s.order_date <=  ‘2021-01-31’
--where s.customer_id  = mb.customer_id AND s.order_date <= ‘2021-01-31’
GROUP BY s.customer_id;

Bonus Q2  - Danny also requires further information about the ranking  of products. He purposedly does not need the ranking of non member purchases so he expects NULL ranking values for customers who are not yet part of the loyalty program.

WITH customers_data AS (
        SELECT
                  s.customer_id, s.order_date, m.product_name, m.price,
        CASE
                 WHEN s.order_date  < mb.join_date THEN ‘N’
                WHEN s.order_date >= mb.join_date THEN ‘Y’
               ELSE ‘N’ END AS member
FROM sales s
LEFT JOIN members mb
ON s.customer_id  =  mb.customer_id
JOIN menu m 
ON  s.product_id  = mb.customer_id
)
SELECT *,
 CASE 
    WHEN member = ‘N’ THEN NULL
ELSE RANK () OVER(
PARTITION BY customer_id, member
ORDER BY order_date) END AS ranking
FROM customers_data
ORDER BY customer_id, order_date;

Insights

• Customer B is the most Frequent visitor with 6 visits in jan 2021.

• Danny’s Diners most popular item is ramen, followed by curry and sushi.

• Customer A loves ramen , Customer C loves only ramen whereas Customer B seems to enjoy sushi, curry and ramen equally.

• The last item ordered by Customers A and B before they became members are sushi and curry. Does it mean both of these items are the deciding factor?

customer-behaviour-analysis-using-mysql's People

Contributors

wyllcodes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.