MediumNo. 2372LeetCode Premium

Calculate the Influence of Each Salesperson — LeetCode 2372 Python Solution

Topics
Database
Reading time
3 min

Table schema

SQLTable schema
Table: Salesperson +----------------+---------+ | Column Name | Type | +----------------+---------+ | salesperson_id | int | | name | varchar | +----------------+---------+ salesperson_id contains unique values. Each row in this table shows the ID of a salesperson.

Example

SQLExample
+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| salesperson_id | int     |
| name           | varchar |
+----------------+---------+
salesperson_id contains unique values.
Each row in this table shows the ID of a salesperson.

Python solution

PythonSolution
import duckdb
import pandas as pd

def solution(salesperson: pd.DataFrame, customer: pd.DataFrame, sales: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Salesperson", salesperson)
    con.register("Customer", customer)
    con.register("Sales", sales)
    return con.execute("""SELECT sp.salesperson_id, name, IFNULL(SUM(price), 0) AS total
FROM
    Salesperson AS sp
    LEFT JOIN Customer AS c ON sp.salesperson_id = c.salesperson_id
    LEFT JOIN Sales AS s ON s.customer_id = c.customer_id
GROUP BY 1;""").df()

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

Frequently asked questions

How hard is LeetCode 2372. Calculate the Influence of Each Salesperson?

LeetCode 2372. Calculate the Influence of Each Salesperson is rated Medium on LeetCode.

What topics does LeetCode 2372. Calculate the Influence of Each Salesperson cover?

LeetCode 2372. Calculate the Influence of Each Salesperson is tagged Database on LeetCode.

Is LeetCode 2372. Calculate the Influence of Each Salesperson a premium problem?

Yes. LeetCode 2372. Calculate the Influence of Each Salesperson is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.