+-------------+------+
| 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.
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.
To generate the invoice, we need to join purchases with product prices, group by invoice and product, and sum the quantities and total price for each product in each invoice. Then, sum the total for each invoice.
WITH invoice_details AS (
SELECT p.invoice_id, p.product_id, SUM(p.quantity) AS quantity,
SUM(p.quantity * pr.price) AS product_total
FROM Purchases p
JOIN Products pr ON p.product_id = pr.product_id
GROUPBY p.invoice_id, p.product_id
),
invoice_totals AS (
SELECT invoice_id, SUM(product_total) AS invoice_total
FROM invoice_details
GROUPBY invoice_id
)
SELECT d.invoice_id, d.product_id, d.quantity, d.product_total, t.invoice_total
FROM invoice_details d
JOIN invoice_totals t ON d.invoice_id = t.invoice_id
ORDERBY d.invoice_id, d.product_id;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WITH invoice_details AS (
SELECT p.invoice_id, p.product_id, SUM(p.quantity) AS quantity,
SUM(p.quantity * pr.price) AS product_total
FROM Purchases p
JOIN Products pr ON p.product_id = pr.product_id
GROUPBY p.invoice_id, p.product_id
),
invoice_totals AS (
SELECT invoice_id, SUM(product_total) AS invoice_total
FROM invoice_details
GROUPBY invoice_id
)
SELECT d.invoice_id, d.product_id, d.quantity, d.product_total, t.invoice_total
FROM invoice_details d
JOIN invoice_totals t ON d.invoice_id = t.invoice_id
ORDERBY d.invoice_id, d.product_id;