cswork / OA打开练习工作台

IntuitOA #10

SQL — Detecting Unusual Product Price Changes

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)SQL

To monitor pricing trends, a company wants to identify products that have had an unusually large price increase. A row qualifies when current_price > 1.5 × average_price (i.e. the current price is more than 50% above the historical average).

Output columns: product_id | name | current_price | average_price.

  • current_price = the latest price row in price_history (the one with the largest changed_at).
  • average_price = average price across the entire dataset for that product, rounded to two decimal places.
  • Sort by average_price DESC, then by product_id ASC.

Schema:

products(id, name, brand) — id PK. price_history(id, product_id, price, changed_at) — id PK, product_id FK referencing products.id, price DECIMAL(10,2), changed_at DATE.

Sample data:

products:

idnamebrand
101Mate Pro XHuawei
102iPhone 11Apple

price_history:

idproduct_idpricechanged_at
2011021000.302023-09-01
2021011020.102023-09-15
2031013500.002023-09-20

Expected output:

product_idnamecurrent_priceaverage_price
101Mate Pro X3500.002260.05

Reasoning: iPhone 11 has only one row (current_price equals average_price), so it fails the > 1.5 × avg threshold. Mate Pro X averages (1020.10 + 3500.00) / 2 = 2260.05, and 3500.00 > 1.5 × 2260.05 = 3390.075, so it qualifies.