Skip to content

Commit 061bb0d

Browse files
Added Trade Pairs
1 parent 904455f commit 061bb0d

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

Trade Pairs/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Restaurant Growth
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+
## 🛠️ Problem Statement
10+
11+
<b>Table Name : Trade_tbl</b>
12+
13+
| Column Name |Type |
14+
| ------------- | ------------- |
15+
| TRADE_ID | varchar(20) |
16+
| Trade_Timestamp | time |
17+
| Trade_Stock | varchar(20) |
18+
| Quantity | int |
19+
| Price | Float |
20+
21+
<b>trade_id is the primary key for this table.</b> </br>
22+
This table contains data about trading of Stocks at some exchange. </br>
23+
</br>
24+
25+
Write a SQL to find trade couples satisfying the following conditions for each stock :
26+
27+
1. The trades should be within 10 seconds of window
28+
2. The prices difference between the trades should be more than 10%
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: Trade_tbl</b>
40+
41+
| TRADE_ID | Trade_Timestamp | Trade_Stock | Quantity | Price |
42+
|----------|-----------------|------------------|----------|-------|
43+
| TRADE1 | 10:01:05 | ITJunction4All | 100 | 20.0 |
44+
| TRADE2 | 10:01:06 | ITJunction4All | 20 | 15.0 |
45+
| TRADE3 | 10:01:08 | ITJunction4All | 150 | 30.0 |
46+
| TRADE4 | 10:01:09 | ITJunction4All | 300 | 32.0 |
47+
| TRADE5 | 10:10:00 | ITJunction4All | -100 | 19.0 |
48+
| TRADE6 | 10:10:01 | ITJunction4All | -300 | 19.0 |
49+
50+
51+
</details>
52+
53+
<details>
54+
<summary>
55+
Output
56+
</summary>
57+
</br>
58+
59+
| tradeid1 | tradeid2 | price_difference |
60+
|----------|----------|------------------|
61+
| TRADE2 | TRADE1 | 25 |
62+
| TRADE3 | TRADE1 | 50 |
63+
| TRADE3 | TRADE2 | 100 |
64+
| TRADE4 | TRADE1 | 60|
65+
| TRADE4 | TRADE2 | 113.3333 |
66+
67+
</details>
68+
69+
---

Trade Pairs/schema.sql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
CREATE TABLE Trade_tbl(
2+
TRADE_ID varchar(20),
3+
Trade_Timestamp time,
4+
Trade_Stock varchar(20),
5+
Quantity int,
6+
Price Float
7+
);
8+
9+
INSERT INTO
10+
Trade_tbl
11+
VALUES
12+
('TRADE1', '10:01:05', 'ITJunction4All', 100, 20);
13+
14+
INSERT INTO
15+
Trade_tbl
16+
VALUES
17+
('TRADE2', '10:01:06', 'ITJunction4All', 20, 15);
18+
19+
INSERT INTO
20+
Trade_tbl
21+
VALUES
22+
('TRADE3', '10:01:08', 'ITJunction4All', 150, 30);
23+
24+
INSERT INTO
25+
Trade_tbl
26+
VALUES
27+
('TRADE4', '10:01:09', 'ITJunction4All', 300, 32);
28+
29+
INSERT INTO
30+
Trade_tbl
31+
VALUES
32+
('TRADE5', '10:10:00', 'ITJunction4All', -100, 19);
33+
34+
INSERT INTO
35+
Trade_tbl
36+
VALUES
37+
('TRADE6', '10:10:01', 'ITJunction4All', -300, 19);
38+
39+
COMMIT;

Trade Pairs/solution.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
WITH cte AS(
2+
SELECT
3+
a.trade_id tradeid1,
4+
b.trade_id tradeid2,
5+
ABS((a.price - b.price) * 100 / b.price) price_difference
6+
FROM
7+
trade_tbl a
8+
INNER JOIN trade_tbl b ON (a.trade_timestamp - INTERVAL '10' SECOND) <= b.trade_timestamp
9+
AND a.trade_timestamp > b.trade_timestamp
10+
AND (
11+
a.price * 1.1 <= b.price
12+
OR a.price * 0.9 >= b.price
13+
)
14+
)
15+
SELECT
16+
*
17+
FROM
18+
cte;

0 commit comments

Comments
 (0)