Generate the Invoice Problem
#2362
Description
LeetCode Problem 2362.
Table: Products
+-------------+------+
| Column Name | Type |
+-------------+------+
| product_id | int |
| price | int |
+-------------+------+
product_id contains unique values.
Each row in this table shows the ID of a product and the price of one unit.
Table: Purchases
+-------------+------+
| Column Name | Type |
+-------------+------+
| invoice_id | int |
| product_id | int |
| quantity | int |
+-------------+------+
(invoice_id, product_id) is the primary key (combination of columns with unique values) for this table.
Each row in this table shows the quantity ordered from one product in an invoice.
Write a solution to show the details of the invoice with the highest price. If two or more invoices have the same price, return the details of the one with the smallest invoice_id.
Return the result table in any order.
The result format is shown in the following example.
Example 1:
Input:
Products table:
+------------+-------+
| product_id | price |
+------------+-------+
| 1 | 100 |
| 2 | 200 |
+------------+-------+
Purchases table:
+------------+------------+----------+
| invoice_id | product_id | quantity |
+------------+------------+----------+
| 1 | 1 | 2 |
| 3 | 2 | 1 |
| 2 | 2 | 3 |
| 2 | 1 | 4 |
| 4 | 1 | 10 |
+------------+------------+----------+
Output:
+------------+----------+-------+
| product_id | quantity | price |
+------------+----------+-------+
| 2 | 3 | 600 |
| 1 | 4 | 400 |
+------------+----------+-------+
Explanation:
Invoice 1: price = (2 * 100) = $200
Invoice 2: price = (4 * 100) + (3 * 200) = $1000
Invoice 3: price = (1 * 200) = $200
Invoice 4: price = (10 * 100) = $1000
The highest price is $1000, and the invoices with the highest prices are 2 and 4. We return the details of the one with the smallest ID, which is invoice 2.
Solution
Approach: Total per invoice, then the best invoice
The work splits into two steps: find which invoice to show, then list its lines.
- Join
PurchasestoProductsand sumquantity * priceperinvoice_id. - Sort those totals by amount descending, then
invoice_idascending, and take one row (LIMIT 1). Sorting by id second handles the tie rule: with equal amounts the smallest id wins. - Join that invoice id back to the purchase lines and output each product with its quantity and the line price
quantity * price.
Note that the output column price is the price of the whole line, not the unit price. In the example, invoices 2 and 4 both total 1000, so invoice 2 is chosen.
The Pandas version computes the line price first, sums it per invoice, and picks the best invoice with the same two-key sort.
WITH invoice_total AS (
SELECT pu.invoice_id, SUM(pu.quantity * pr.price) AS total
FROM Purchases AS pu
JOIN Products AS pr ON pr.product_id = pu.product_id
GROUP BY pu.invoice_id
), best AS (
SELECT invoice_id
FROM invoice_total
ORDER BY total DESC, invoice_id ASC
LIMIT 1
)
SELECT pu.product_id,
pu.quantity,
pu.quantity * pr.price AS price
FROM Purchases AS pu
JOIN Products AS pr ON pr.product_id = pu.product_id
JOIN best AS b ON b.invoice_id = pu.invoice_id;