返回介绍

solution / 1500-1599 / 1501.Countries You Can Safely Invest In / README_EN

发布于 2024-06-17 01:03:19 字数 5466 浏览 0 评论 0 收藏 0

1501. Countries You Can Safely Invest In

中文文档

Description

Table Person:

+----------------+---------+
| Column Name  | Type  |
+----------------+---------+
| id       | int   |
| name       | varchar |
| phone_number   | varchar |
+----------------+---------+
id is the column of unique values for this table.
Each row of this table contains the name of a person and their phone number.
Phone number will be in the form 'xxx-yyyyyyy' where xxx is the country code (3 characters) and yyyyyyy is the phone number (7 characters) where x and y are digits. Both can contain leading zeros.

 

Table Country:

+----------------+---------+
| Column Name  | Type  |
+----------------+---------+
| name       | varchar |
| country_code   | varchar |
+----------------+---------+
country_code is the column of unique values for this table.
Each row of this table contains the country name and its code. country_code will be in the form 'xxx' where x is digits.

 

Table Calls:

+-------------+------+
| Column Name | Type |
+-------------+------+
| caller_id   | int  |
| callee_id   | int  |
| duration  | int  |
+-------------+------+
This table may contain duplicate rows.
Each row of this table contains the caller id, callee id and the duration of the call in minutes. caller_id != callee_id

 

A telecommunications company wants to invest in new countries. The company intends to invest in the countries where the average call duration of the calls in this country is strictly greater than the global average call duration.

Write a solution to find the countries where this company can invest.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Person table:
+----+----------+--------------+
| id | name   | phone_number |
+----+----------+--------------+
| 3  | Jonathan | 051-1234567  |
| 12 | Elvis  | 051-7654321  |
| 1  | Moncef   | 212-1234567  |
| 2  | Maroua   | 212-6523651  |
| 7  | Meir   | 972-1234567  |
| 9  | Rachel   | 972-0011100  |
+----+----------+--------------+
Country table:
+----------+--------------+
| name   | country_code |
+----------+--------------+
| Peru   | 051      |
| Israel   | 972      |
| Morocco  | 212      |
| Germany  | 049      |
| Ethiopia | 251      |
+----------+--------------+
Calls table:
+-----------+-----------+----------+
| caller_id | callee_id | duration |
+-----------+-----------+----------+
| 1     | 9     | 33     |
| 2     | 9     | 4    |
| 1     | 2     | 59     |
| 3     | 12    | 102    |
| 3     | 12    | 330    |
| 12    | 3     | 5    |
| 7     | 9     | 13     |
| 7     | 1     | 3    |
| 9     | 7     | 1    |
| 1     | 7     | 7    |
+-----------+-----------+----------+
Output: 
+----------+
| country  |
+----------+
| Peru   |
+----------+
Explanation: 
The average call duration for Peru is (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667
The average call duration for Israel is (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500
The average call duration for Morocco is (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000 
Global call duration average = (2 * (33 + 4 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000
Since Peru is the only country where the average call duration is greater than the global average, it is the only recommended country.

Solutions

Solution 1: Equi-Join + Group By + Subquery

We can use an equi-join to join the Person table and the Calls table on the condition of Person.id = Calls.caller_id or Person.id = Calls.callee_id, and then join the result with the Country table on the condition of left(phone_number, 3) = country_code. After that, we can group by country and calculate the average call duration for each country. Finally, we can use a subquery to find the countries whose average call duration is greater than the global average call duration.

# Write your MySQL query statement below
SELECT country
FROM
  (
    SELECT c.name AS country, AVG(duration) AS duration
    FROM
      Person
      JOIN Calls ON id IN(caller_id, callee_id)
      JOIN Country AS c ON LEFT(phone_number, 3) = country_code
    GROUP BY 1
  ) AS t
WHERE duration > (SELECT AVG(duration) FROM Calls);

Solution 2

# Write your MySQL query statement below
WITH
  T AS (
    SELECT c.name AS country, AVG(duration) AS duration
    FROM
      Person
      JOIN Calls ON id IN(caller_id, callee_id)
      JOIN Country AS c ON LEFT(phone_number, 3) = country_code
    GROUP BY 1
  )
SELECT country
FROM T
WHERE duration > (SELECT AVG(duration) FROM Calls);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文