File tree Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Price on a given Date
2
+ ![ Star Badge] ( https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99 )
3
+ [ ![ View Main Folder] ( https://img.shields.io/badge/View-Main_Folder-971901? )] ( https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main )
4
+ [ ![ View Repositories] ( https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub )] ( https://github.com/thecoddiwompler?tab=repositories )
5
+ [ ![ View My Profile] ( https://img.shields.io/badge/View-My_Profile-green?logo=GitHub )] ( https://github.com/thecoddiwompler )
6
+
7
+ ---
8
+
9
+
10
+ ## 🛠️ Problem Statement
11
+
12
+ <b >Table Name : sku</b >
13
+
14
+ | Column Name | Type |
15
+ | ------------- | ------------- |
16
+ | sku_id | INT |
17
+ | price_date | DATE |
18
+ | price | INT |
19
+
20
+ <b >sku_id, price_date is the primary key of this table.</b >
21
+ </br >
22
+
23
+ You are given the price of each sku whenever there is a change in price.
24
+
25
+ </br >
26
+
27
+ Write an SQL to find the price at the start of each month. Refer to the Example Output below for the desired format.
28
+
29
+ </br >
30
+
31
+ The query result format is in the following example.
32
+
33
+ <details >
34
+ <summary >
35
+ Input
36
+ </summary >
37
+ </br >
38
+
39
+ <b > Table Name: sku </b ></br >
40
+
41
+ | sku_id | price_date | price |
42
+ | --------| -------------| -------|
43
+ | 1 | 2023-01-01 | 10 |
44
+ | 1 | 2023-02-15 | 15 |
45
+ | 1 | 2023-03-03 | 18 |
46
+ | 1 | 2023-03-27 | 15 |
47
+ | 1 | 2023-04-06 | 20 |
48
+
49
+
50
+
51
+ </details >
52
+
53
+ <details >
54
+ <summary >
55
+ Output
56
+ </summary >
57
+ </br >
58
+
59
+ | sku_id | month_start_date | price |
60
+ | --------| -------------------| -------|
61
+ | 1 | 2023-01-01 | 10 |
62
+ | 1 | 2023-02-01 | 10 |
63
+ | 1 | 2023-03-01 | 15 |
64
+ | 1 | 2023-04-01 | 15 |
65
+ | 1 | 2023-05-01 | 20 |
66
+
67
+
68
+ </details >
69
+
70
+ ---
Original file line number Diff line number Diff line change
1
+ CREATE TABLE sku (sku_id int , price_date date , price int );
2
+
3
+ INSERT INTO
4
+ sku
5
+ VALUES
6
+ (1 , ' 2023-01-01' , 10 ),
7
+ (1 , ' 2023-02-15' , 15 ),
8
+ (1 , ' 2023-03-03' , 18 ),
9
+ (1 , ' 2023-03-27' , 15 ),
10
+ (1 , ' 2023-04-06' , 20 );
11
+
12
+ COMMIT ;
Original file line number Diff line number Diff line change
1
+ WITH cte AS (
2
+ SELECT
3
+ DISTINCT date_trunc(' Month' , price_date) month_start_date
4
+ FROM
5
+ sku
6
+ UNION
7
+ SELECT
8
+ date_trunc(' Month' , MAX (price_date)) + INTERVAL ' 1' MONTH
9
+ FROM
10
+ sku
11
+ ),
12
+ sol AS (
13
+ SELECT
14
+ b .sku_id ,
15
+ a .month_start_date ,
16
+ b .price_date ,
17
+ b .price
18
+ FROM
19
+ cte a
20
+ INNER JOIN sku b ON a .month_start_date >= b .price_date
21
+ ),
22
+ ans AS (
23
+ SELECT
24
+ sku_id,
25
+ month_start_date,
26
+ MAX (price_date) target_date
27
+ FROM
28
+ sol
29
+ GROUP BY
30
+ sku_id,
31
+ month_start_date
32
+ )
33
+ SELECT
34
+ a .sku_id ,
35
+ TO_CHAR(a .month_start_date , ' YYYY-MM-DD' ) month_start_date,
36
+ a .price
37
+ FROM
38
+ sol a
39
+ INNER JOIN ans b ON a .sku_id = b .sku_id
40
+ AND a .month_start_date = b .month_start_date
41
+ AND a .price_date = b .target_date ;
You can’t perform that action at this time.
0 commit comments