LeetCode //MySQL - 1251. Average Selling Price

1251. Average Selling Price

Table: Prices

±--------------±--------+

| Column Name | Type |

±--------------±--------+

| product_id | int |

| start_date | date |

| end_date | date |

| price | int |

±--------------±--------+

(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table.

Each row of this table indicates the price of the product_id in the period from start_date to end_date.

For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.

Table: UnitsSold

±--------------±--------+

| Column Name | Type |

±--------------±--------+

| product_id | int |

| purchase_date | date |

| units | int |

±--------------±--------+

This table may contain duplicate rows.

Each row of this table indicates the date, units, and product_id of each product sold.

Write a solution to find the average selling price for each product. average_price should be rounded to 2 decimal places. If a product does not have any sold units, its average selling price is assumed to be 0.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

Prices table:

±-----------±-----------±-----------±-------+

| product_id | start_date | end_date | price |

±-----------±-----------±-----------±-------+

| 1 | 2019-02-17 | 2019-02-28 | 5 |

| 1 | 2019-03-01 | 2019-03-22 | 20 |

| 2 | 2019-02-01 | 2019-02-20 | 15 |

| 2 | 2019-02-21 | 2019-03-31 | 30 |

±-----------±-----------±-----------±-------+

UnitsSold table:

±-----------±--------------±------+

| product_id | purchase_date | units |

±-----------±--------------±------+

| 1 | 2019-02-25 | 100 |

| 1 | 2019-03-01 | 15 |

| 2 | 2019-02-10 | 200 |

| 2 | 2019-03-22 | 30 |

±-----------±--------------±------+

Output:

±-----------±--------------+

| product_id | average_price |

±-----------±--------------+

| 1 | 6.96 |

| 2 | 16.96 |

±-----------±--------------+

Explanation: Average selling price = Total Price of Product / Number of products sold.

Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96

Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96

From: LeetCode

Link: 1251. Average Selling Price


Solution:

Ideas:
  • Join Prices and UnitsSold on:
    • same product_id
    • purchase_date within start_date and end_date
  • For each product:
    • Compute total revenue = SUM(price * units)
    • Compute total units sold = SUM(units)
    • Average price = total revenue / total units
  • Use LEFT JOIN so products with no sales are included.
  • Use IFNULL(..., 0) to return 0 when a product has no sold units.
  • Round the result to 2 decimal places using ROUND().
Code:
sql 复制代码
# Write your MySQL query statement below
SELECT
    p.product_id,
    IFNULL(ROUND(SUM(p.price * u.units) / SUM(u.units), 2), 0) AS average_price
FROM Prices p
LEFT JOIN UnitsSold u
    ON p.product_id = u.product_id
   AND u.purchase_date BETWEEN p.start_date AND p.end_date
GROUP BY p.product_id;
相关推荐
FLJwu1 小时前
运动相机采购实战|第四篇(终章):一张清单走完选型全流程,把风险拦在源头
c语言·开发语言·数码相机·运动相机·oem 量产
牧瀬クリスだ1 小时前
Redis Hash实战:高效存储对象与购物车
java·数据库·redis·算法·哈希算法
文丑颜不良啊1 小时前
LeetCode1141~1160题目解析
java·leetcode
木井巳2 小时前
【动态规划: 路径问题】下降路径最小和
java·算法·leetcode·动态规划·推荐算法
deepseek232 小时前
OpenAI三项科学发现拆解:3-SAT亚指数算法挑战ETH,AI科学声明的验证流水线怎么搭
人工智能·算法·openai
梦帮科技2 小时前
基于幺正李代数连续归一化流的混合量子生成模型 (HQNN-Flow) 完整技术指南(上部分)
网络·人工智能·算法·量子计算
青少儿编程课堂2 小时前
蓝桥杯青少组 + 全国青少年信息素养大赛 + 电子学会图形化考级|校园推箱子大挑战解析
c++·python·算法·bfs·信息学竞赛
ynchyong2 小时前
培养程序化思维
leetcode·程序化思维·刷题经验
荆棘鸟智能3 小时前
河道采砂船识别与轨迹追踪怎么做?检测、跟踪与电子围栏实战思路
人工智能·算法·目标检测