Skip to content

Commit d0948bb

Browse files
Added Messages Exchanged
1 parent 16393a4 commit d0948bb

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Messages Exchanged/READme.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Messages Exchanged
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+
12+
<b>Table Name : subscriber</b>
13+
</br>
14+
| Column Name |Type |
15+
| ------------- | ------------- |
16+
| sms_date | date |
17+
| sender | varchar(20) |
18+
| receiver | varchar(20) |
19+
| sms_no | int |
20+
21+
Each row contains information about the messages exchanged between two people on the given sms_date.
22+
23+
24+
Write a SQL query to fnd the total number of messages exchanged between each person each day in the "subscriber" table.
25+
</br>
26+
</br>
27+
Return the ouput in any order. <br><br>
28+
<b>The query result format is in the following example: </b>
29+
</br>
30+
</br>
31+
32+
<details>
33+
<summary>
34+
Input
35+
</summary>
36+
<br>
37+
38+
<b>Table Name : subscriber</b>
39+
40+
| sms_date | sender | receiver | sms_no |
41+
|------------|----------|----------|--------|
42+
| 2020-04-01 | Avinash | Vibhor | 10 |
43+
| 2020-04-01 | Vibhor | Avinash | 20 |
44+
| 2020-04-01 | Avinash | Pawan | 30 |
45+
| 2020-04-01 | Pawan | Avinash | 20 |
46+
| 2020-04-01 | Vibhor | Pawan | 5 |
47+
| 2020-04-01 | Pawan | Vibhor | 8 |
48+
| 2020-04-01 | Vibhor | Deepak | 50 |
49+
50+
51+
</details>
52+
53+
<details>
54+
<summary>
55+
Output
56+
</summary>
57+
<br>
58+
59+
| sms_date | p1 | p2 | total_sms |
60+
|------------|----------|---------|-----------|
61+
| 2020-04-01 | Deepak | Avinash | 0 |
62+
| 2020-04-01 | Pawan | Avinash | 50 |
63+
| 2020-04-01 | Pawan | Deepak | 0 |
64+
| 2020-04-01 | Vibhor | Avinash | 30 |
65+
| 2020-04-01 | Vibhor | Deepak | 50 |
66+
| 2020-04-01 | Vibhor | Pawan | 13 |
67+
68+
69+
</details>
70+
71+
---

Messages Exchanged/schema.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE TABLE subscriber (
2+
sms_date date,
3+
sender varchar(20),
4+
receiver varchar(20),
5+
sms_no int
6+
);
7+
8+
-- insert some values
9+
10+
INSERT INTO
11+
subscriber (date, sender, receiver, amount)
12+
VALUES
13+
('2020-4-1', 'Avinash', 'Vibhor', 10),
14+
('2020-4-1', 'Vibhor', 'Avinash', 20),
15+
('2020-4-1', 'Avinash', 'Pawan', 30),
16+
('2020-4-1', 'Pawan', 'Avinash', 20),
17+
('2020-4-1', 'Vibhor', 'Pawan', 5),
18+
('2020-4-1', 'Pawan', 'Vibhor', 8),
19+
('2020-4-1', 'Vibhor', 'Deepak', 50);
20+
21+
COMMIT;

Messages Exchanged/solution.sql

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
WITH recursive all_dates AS (
2+
SELECT
3+
MIN(sms_date) sms_date
4+
FROM
5+
subscriber
6+
UNION
7+
SELECT
8+
sms_date + 1
9+
FROM
10+
all_dates
11+
WHERE
12+
sms_date + 1 <=(
13+
SELECT
14+
MAX(sms_date)
15+
FROM
16+
subscriber
17+
)
18+
),
19+
person AS (
20+
SELECT
21+
DISTINCT sender person
22+
FROM
23+
subscriber
24+
UNION
25+
SELECT
26+
DISTINCT receiver
27+
FROM
28+
subscriber
29+
),
30+
all_person AS (
31+
SELECT
32+
person,
33+
ROW_NUMBER() over(
34+
ORDER BY
35+
person
36+
) rn
37+
FROM
38+
person
39+
),
40+
cte AS (
41+
SELECT
42+
c.sms_date,
43+
a.person p1,
44+
b.person p2
45+
FROM
46+
all_person a
47+
INNER JOIN all_person b ON a.rn > b.rn
48+
CROSS JOIN all_dates c
49+
)
50+
SELECT
51+
a.sms_date,
52+
a.p1,
53+
a.p2,
54+
COALESCE(sum(sms_no), 0) total_sms
55+
FROM
56+
cte a
57+
LEFT OUTER JOIN subscriber b ON a.sms_date = b.sms_date
58+
AND (
59+
(
60+
a.p1 = b.sender
61+
AND a.p2 = b.receiver
62+
)
63+
OR (
64+
a.p2 = b.sender
65+
AND a.p1 = b.receiver
66+
)
67+
)
68+
GROUP BY
69+
a.sms_date,
70+
a.p1,
71+
a.p2;

0 commit comments

Comments
 (0)