-
Notifications
You must be signed in to change notification settings - Fork 43
/
number-of-calls-between-two-persons.sql
118 lines (100 loc) · 2.99 KB
/
number-of-calls-between-two-persons.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Number Of Calls Between Two Persons Problem
Description
LeetCode Problem 1699.
Table: Calls
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| from_id | int |
| to_id | int |
| duration | int |
+-------------+---------+
This table does not have a primary key, it may contain duplicates.
This table contains the duration of a phone call between from_id and to_id.
from_id != to_id
Write an SQL query to report the number of calls and the total call duration between each pair of distinct persons (person1, person2) where person1 < person2.
Return the result table in any order.
The query result format is in the following example:
Calls table:
+---------+-------+----------+
| from_id | to_id | duration |
+---------+-------+----------+
| 1 | 2 | 59 |
| 2 | 1 | 11 |
| 1 | 3 | 20 |
| 3 | 4 | 100 |
| 3 | 4 | 200 |
| 3 | 4 | 200 |
| 4 | 3 | 499 |
+---------+-------+----------+
Result table:
+---------+---------+------------+----------------+
| person1 | person2 | call_count | total_duration |
+---------+---------+------------+----------------+
| 1 | 2 | 2 | 70 |
| 1 | 3 | 1 | 20 |
| 3 | 4 | 4 | 999 |
+---------+---------+------------+----------------+
Users 1 and 2 had 2 calls and the total duration is 70 (59 + 11).
Users 1 and 3 had 1 call and the total duration is 20.
Users 3 and 4 had 4 calls and the total duration is 999 (100 + 200 + 200 + 499).
*/
# V0
# IDEA : cte + union all
WITH cte AS
(SELECT *
FROM Calls c1
UNION ALL
SELECT
to_id,
from_id,
duration
FROM Calls c2)
SELECT from_id AS person1,
to_id AS person2,
COUNT(1) AS call_count,
SUM(duration) AS total_duration
FROM cte
WHERE to_id > from_id
GROUP BY person1,
person2
# V1
# https://circlecoder.com/number-of-calls-between-two-persons/
select from_id as person1,to_id as person2,
count(duration) as call_count, sum(duration) as total_duration
from (select *
from Calls
union all
select to_id, from_id, duration
from Calls) t1
where from_id < to_id
group by person1, person2
# V1'
# https://leetcode.ca/2020-07-25-1699-Number-of-Calls-Between-Two-Persons/
WITH caller as (
select from_id as person1, to_id as person2, duration
from Calls
UNION ALL
select to_id as person1, from_id as person2, duration
from Calls
),
unique_caller as (
select person1, person2, duration
from caller
where person1 < person2
)
select
person1, person2, count(*) as call_count, sum(duration) as total_duration
from unique_caller
group by person1, person2
# V2
# Time: O(n)
# Space: O(n)
SELECT LEAST(from_id,to_id) as person1,
GREATEST(from_id,to_id) as person2,
COUNT(*) as call_count,
SUM(duration) as total_duration
FROM Calls
GROUP BY person1, person2
ORDER BY NULL;