Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 91a9972

Browse files
product-price-at-a-given-date.sql
1 parent 32bc0c0 commit 91a9972

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

‎MySQL/product-price-at-a-given-date.sql‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1+
/*
12
# Time: O(mlogn), m is the number of unique product id, n is the number of changed dates
23
# Space: O(m)
4+
*/
5+
6+
SELECT
7+
t1.product_id AS product_id,
8+
IF(ISNULL(t2.price), 10, t2.price) AS price
9+
FROM
10+
(SELECT DISTINCT product_id
11+
FROM products) AS t1
12+
LEFT JOIN
13+
(SELECT
14+
product_id,
15+
new_price AS price
16+
FROM
17+
products
18+
WHERE
19+
(product_id, change_date) IN
20+
(SELECT
21+
product_id,
22+
MAX(change_date)
23+
FROM
24+
products
25+
WHERE
26+
change_date <= '2019年08月16日'
27+
GROUP BY
28+
product_id)
29+
) AS t2
30+
ON
31+
t1.product_id = t2.product_id;
332

4-
SELECT t1.product_id AS product_id,
5-
IF(Isnull(t2.price), 10, t2.price) AS price
6-
FROM (SELECT DISTINCT product_id
7-
FROM products) AS t1
8-
left join (SELECT product_id,
9-
new_price AS price
10-
FROM products
11-
WHERE ( product_id, change_date ) IN (SELECT product_id,
12-
Max(change_date)
13-
FROM products
14-
WHERE change_date <= '2019年08月16日'
15-
GROUP BY product_id))
16-
AS t2
17-
ON t1.product_id = t2.product_id

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /