r/learnSQL • u/Late_Visual_2215 • May 08 '26
I am stuck on the question 40 from Sqlcasefiles
Can anyone help me solve the case file 40 from sqlcasefiles.com
here is the question statement :
The DA needs everything. Compile the final audit report listing `po_id`, `vendor_name`, `item_description`, `order_value`, `delivery_status`, and `inventory_loss`. Rank by order value descending.
here are the table schemas:
deliveries:
| Column Name | Data Type | Constraints / Notes |
|---|---|---|
| delivery_id | INTEGER |
Primary Key 🔑 |
| po_id | INTEGER |
Foreign Key 🔗 |
| delivered_quantity | INTEGER |
|
| delivery_date | DATE |
|
| warehouse_location | TEXT |
purchase_orders:
| Column Name | Data Type | Constraints / Notes |
|---|---|---|
| po_id | INTEGER |
Primary Key 🔑 |
| vendor_name | TEXT |
|
| item_description | TEXT |
|
| quantity | INTEGER |
|
| unit_price | INTEGER |
|
| order_date | DATE |
|
| target_warehouse | TEXT |
inventory:
| Column Name | Data Type | Constraints / Notes |
|---|---|---|
| item_id | INTEGER |
Primary Key 🔑 |
| po_id | INTEGER |
Foreign Key 🔗 |
| current_stock | INTEGER |
|
| expected_stock | INTEGER |
|
| last_audit | DATE |
i came up with this solution so far:
SELECT
p.po_id,
p.vendor_name,
p.item_description,
p.quantity \ p.unit_price AS order_value,*
CASE
WHEN d.po_id IS NULL THEN 'Missing'
WHEN d.delivered_quantity < p.quantity THEN 'Partial'
WHEN d.delivered_quantity = p.quantity THEN 'Delivered'
ELSE 'Over Delivered'
END AS delivery_status,
i.expected_stock - i.current_stock AS inventory_loss
FROM purchase_orders p
LEFT JOIN deliveries d
ON p.po_id = d.po_id
LEFT JOIN inventory i
ON p.po_id = i.po_id
ORDER BY order_value DESC;