SQL MOSAIC # 1699. Number of Calls Between Two Persons

Summer Nie
Jul 5, 2022

7/5/2022

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.

Thinking Pathway:

  1. This question creates a temporary table.
  2. Syntax:

WITH temporaryTable (averageValue) as
(SELECT avg(Attr1)
FROM Table)
SELECT Attr1
FROM Table, temporaryTable
WHERE Table.Attr1 > temporaryTable.averageValue;

3. You need to create syntax right after you create a temporary table. Running the temporary table only will return errors.

Solution(s):

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response