网站建站优化,和国外做贸易用什么网站,甘肃兰州建筑网,怎么建设网站网站力扣题
1、题目地址
1699. 两人之间的通话次数
2、模拟表
表#xff1a;Calls
Column NameTypefrom_idintto_idintdurationint
该表没有主键(具有唯一值的列)#xff0c;它可能包含重复项。该表包含 from_id 与 to_id 间的一次电话的时长。from_id ! to_id
3、要求
编…力扣题
1、题目地址
1699. 两人之间的通话次数
2、模拟表
表Calls
Column NameTypefrom_idintto_idintdurationint
该表没有主键(具有唯一值的列)它可能包含重复项。该表包含 from_id 与 to_id 间的一次电话的时长。from_id ! to_id
3、要求
编写解决方案统计每一对用户 (person1, person2) 之间的通话次数和通话总时长其中 person1 person2 。
以 任意顺序 返回结果表。
返回结果格式如下示例所示。
示例 1
输入
Calls 表
from_idto_idduration12592111132034100342003420043499
输出
person1person2call_counttotal_duration1227013120344999
解释 用户 1 和 2 打过 2 次电话总时长为 70 (59 11)。 用户 1 和 3 打过 1 次电话总时长为 20。 用户 3 和 4 打过 4 次电话总时长为 999 (100 200 200 499)。
4、代码编写
分组使用原代码非别名
SELECT IF(from_id to_id, from_id, to_id) AS person1, IF(to_id from_id, to_id, from_id) AS person2,COUNT(*) AS call_count, SUM(duration) AS total_duration
FROM Calls
GROUP BY IF(from_id to_id, from_id, to_id), IF(to_id from_id, to_id, from_id)分组使用别名
SELECT IF(from_id to_id, from_id, to_id) AS person1, IF(to_id from_id, to_id, from_id) AS person2,COUNT(*) AS call_count, SUM(duration) AS total_duration
FROM Calls
GROUP BY person1, person2分组使用数字对应原代码查询位置1开始递增
SELECT IF(from_id to_id, from_id, to_id) AS person1, IF(to_id from_id, to_id, from_id) AS person2,COUNT(*) AS call_count, SUM(duration) AS total_duration
FROM Calls
GROUP BY 1, 2SELECT COUNT(*) AS call_count, SUM(duration) AS total_duration,IF(from_id to_id, from_id, to_id) AS person1, IF(to_id from_id, to_id, from_id) AS person2
FROM Calls
GROUP BY 3, 4