题目链接:点击打开链接
题目大意:略。
解题思路:注意 LEAST & GREATEST 函数。
AC 代码
-- 解决方案(1)
WITH t1 AS(SELECT * FROM Calls WHERE from_id < to_id
UNION ALL
SELECT to_id, from_id, duration FROM Calls WHERE from_id > to_id)
SELECT from_id person1, to_id person2, COUNT(*) call_count, SUM(duration) total_duration
FROM t1
GROUP BY from_id, to_id
-- 解决方案(2)
SELECT from_id person1, to_id person2, COUNT(*) call_count, SUM(duration) total_duration
FROM Calls
GROUP BY LEAST(from_id, to_id), GREATEST(from_id, to_id)