• 1699. 两人之间的通话次数


    SQL架构

    表: Calls

    +-------------+---------+
    | Column Name | Type    |
    +-------------+---------+
    | from_id     | int     |
    | to_id       | int     |
    | duration    | int     |
    +-------------+---------+
    该表没有主键,可能存在重复项。
    该表包含 from_id 与 to_id 间的一次电话的时长。
    from_id != to_id
    

    编写 SQL 语句,查询每一对用户 (person1, person2) 之间的通话次数和通话总时长,其中 person1 < person2 。

    以 任意顺序 返回结果表。

    查询结果格式如下示例所示。

    示例 1:

    输入:
    Calls 表:
    +---------+-------+----------+
    | 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      |
    +---------+-------+----------+
    输出:
    +---------+---------+------------+----------------+
    | person1 | person2 | call_count | total_duration |
    +---------+---------+------------+----------------+
    | 1       | 2       | 2          | 70             |
    | 1       | 3       | 1          | 20             |
    | 3       | 4       | 4          | 999            |
    +---------+---------+------------+----------------+
    解释:
    用户 1 和 2 打过 2 次电话,总时长为 70 (59 + 11)。
    用户 1 和 3 打过 1 次电话,总时长为 20。
    用户 3 和 4 打过 4 次电话,总时长为 999 (100 + 200 + 200 + 499)。

    用的 abs(  )  和 +:

    1. select
    2. min(from_id) person1,max(to_id) person2,count(duration) call_count,sum(duration) total_duration
    3. #分组后会有 类似于 1,2 或者 2,1的数据 用 min() 和 max() 保证 person1 < person2
    4. from
    5. Calls
    6. group by abs(from_id-to_id),from_id+to_id #两数之差的绝对值相同,且两数之和相同,则两个数的组合是唯一的

    用的 if:

    1. SELECT
    2. person1,person2,
    3. count(*) call_count,
    4. sum(duration) total_duration
    5. FROM (
    6. SELECT
    7. IF(from_id>to_id, to_id, from_id) person1,
    8. IF(from_id>to_id,from_id,to_id) person2,
    9. duration
    10. FROM calls
    11. ) c
    12. GROUP BY
    13. person1, person2

    union all:

    1. SELECT tmp.person1,tmp.person2,COUNT(1) call_count,SUM(duration) total_duration FROM(
    2. SELECT from_id person1,to_id person2,duration FROM calls WHERE from_id<to_id
    3. UNION ALL
    4. SELECT to_id person1,from_id person2,duration FROM calls WHERE from_id>to_id
    5. ) tmp
    6. GROUP BY tmp.person1,tmp.person2

    用的least() 和 greatest():

    1. select
    2. from_id as person1,
    3. to_id as person2,
    4. count(1) as call_count,
    5. sum(duration) as total_duration
    6. from calls
    7. group by least(from_id, to_id),greatest(from_id, to_id)

    笔记:

    least() 几个字段 取最小 和 greatest() 几个字段 取最大

  • 相关阅读:
    simpledateformat怎么改变格式 SimpleDateFormat 的使用及其 注意事项
    SQL必需掌握的100个重要知识点:使用子查询
    【论文解读系列】NER方向:LatticeLSTM (ACL2018)
    避坑指南!!在树莓派4b上安装Pycharm以及无法使用终端的问题解决!!
    SQLSERVER 临时表和表变量到底有什么区别?
    数据中台的五个关键要素
    2023年【起重机械指挥】考试题库及起重机械指挥证考试
    Kotlin 优点
    结构型设计模式
    浅谈Unity UI适配(二)
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125565824