SQL Series #2: Calculating Loyalty Points with INTERVAL

SQL Series #2: Calculating Loyalty Points with INTERVAL


Problem Statement

In the first week after joining (including the join date), customers earn 2x points on all items, not just sushi. Normally, each $1 spent equates to 10 points, with just sushi earning a 2x multiplier. How many points do members accumulate by the end of January 2021?


The following are the input tables:

sales

members

menu


Expected Output



Approach

The solution utilizes INTERVAL concept. How it Works:

  • Add or Subtract Time: You can use INTERVAL to add or subtract a specified amount of time to a date. For example, join_date + INTERVAL '7 days' adds 7 days to the join_date.
  • Dynamic Date Calculations: It enables you to set dynamic conditions based on time. For instance, determining if an order was placed within the first 7 days after a customer joined a loyalty program.


Here are the steps to reach the solution:

?? Step 1: Calculate Points for Each Order

Each customer's order is evaluated to calculate points:

  • In the first 7 days after a customer joins, they earn double points for every dollar spent.
  • After the first week, they earn double points for sushi orders and standard points for other products.

?? Step 2: Sum Up Points Per Customer

The points from all orders are then summed up for each customer, giving us their total points earned over the specified period.

?? Step 3: Generate the Final Output

The final result is a table showing each customer’s total loyalty points based on their order history, considering the special point rules.


Here is how the points are calculated:

Customer 1:

2021-01-05 (Join date + 4 days): 'sushi' → 20 × 2 × 10 = 400 20×2×10 = 400 points

2021-01-10: 'burger' → 15 × 10 = 150 15×10 = 150 points

Customer 2:

2021-01-15 (Join date + 5 days): 'pasta' → 12 × 2 × 10 = 240 12×2×10 = 240 points

2021-01-20: 'sushi' → 20 × 2 × 10 = 400 20×2×10 = 400 points


Full Solution Code




Sanchi Jain

Data Science | AI, ML | Business Intelligence | 3+ years of healthcare experience | Quality & Reg Affairs | Pharmaceutical Sciences and Technology

7 个月

Very informative and written in a structured manner

要查看或添加评论,请登录

Nischal Subedi的更多文章

  • SQL SERIES #4: LEAD() Window Function

    SQL SERIES #4: LEAD() Window Function

    Problem Statement Given a table sales_data that logs daily sales figures for multiple stores, the goal is to calculate…

    1 条评论
  • SQL Series #3: ROW_NUMBER() Window Function

    SQL Series #3: ROW_NUMBER() Window Function

    Problem Statement: Imagine you work for a retail company, and you want to analyze the performance of your salespeople…

  • AWS AI Service: Amazon Rekognition

    AWS AI Service: Amazon Rekognition

    Amazon Rekognition is an AI service that enables users to effortlessly incorporate image and video analysis into their…

  • SQL Series #1: Dense_Rank() Window Function

    SQL Series #1: Dense_Rank() Window Function

    ?? Problem Statement Write a query that outputs the name of the credit card and how many cards were issued in its…

社区洞察

其他会员也浏览了