67. CRM Automotive Sales Analysis

In a CRM-based automotive sales environment, managers often need to analyze the number of unique customer leads and partner interactions across different products (make_name) per day. This helps evaluate marketing reach and channel performance.

You are given a table DailySales:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘   date_id   ā•‘   date   ā•‘
║─────────────┼──────────║
ā•‘  make_name  ā•‘ varchar  ā•‘
║─────────────┼──────────║
ā•‘   lead_id   ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘ partner_id  ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • date_id: Date of the sale.
  • make_name: The brand of the car (only in lower english letters).
  • lead_id: ID representing a potential customer lead.
  • partner_id: ID representing a sales partner or channel.

Write a query to find, for each date_id and make_name, the count of distinct lead_id's and distinct partner_id's.

Return the result table ordered by date_id and make_name in ascending order.

Example 1:

Example:

Input:

DailySales

╔═══════════╦═══════════╦═════════╦════════════╗
ā•‘  date_id  ā•‘ make_name ā•‘ lead_id ā•‘ partner_id ā•‘
╠═══════════╬═══════════╬═════════╬════════════╣
ā•‘ 2020-12-8 ā•‘  toyota   ā•‘    0    ā•‘     1      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-8 ā•‘  toyota   ā•‘    1    ā•‘     0      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-8 ā•‘  toyota   ā•‘    1    ā•‘     2      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-7 ā•‘  toyota   ā•‘    0    ā•‘     2      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-7 ā•‘  toyota   ā•‘    0    ā•‘     1      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-8 ā•‘  honda    ā•‘    1    ā•‘     2      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-8 ā•‘  honda    ā•‘    2    ā•‘     1      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-7 ā•‘  honda    ā•‘    0    ā•‘     1      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-7 ā•‘  honda    ā•‘    1    ā•‘     2      ā•‘
║───────────┼───────────┼─────────┼────────────║
ā•‘ 2020-12-7 ā•‘  honda    ā•‘    2    ā•‘     1      ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Expected Output:

╔═══════════╦═════════════╦══════════════╦═════════════════╗
ā•‘  date_id  ā•‘   make_name ā•‘ unique_leads ā•‘ unique_partners ā•‘
╠═══════════╬═════════════╬══════════════╬═════════════════╣
ā•‘ 2020-12-7 ā•‘    honda    ā•‘      3       ā•‘        2        ā•‘
║───────────┼─────────────┼──────────────┼─────────────────║
ā•‘ 2020-12-7 ā•‘   toyota    ā•‘      1       ā•‘        2        ā•‘
║───────────┼─────────────┼──────────────┼─────────────────║
ā•‘ 2020-12-8 ā•‘    honda    ā•‘      2       ā•‘        2        ā•‘
║───────────┼─────────────┼──────────────┼─────────────────║
ā•‘ 2020-12-8 ā•‘   toyota    ā•‘      2       ā•‘        3        ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

For each group:

Count how many distinct lead_ids and partner_ids occurred on that date for that car make.

Example: For 2020-12-8, toyota has leads [0, 1] and partners [0, 1, 2] → counts = 2, 3.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

DailySales