0
点赞
收藏
分享

微信扫一扫

LeetCode(数据库)- First and Last Call On the Same Day


题目链接:​​点击打开链接​​

题目大意:略。

解题思路:注意是‘每天’的第一个和最后一个电话。

AC 代码

with a as (
SELECT caller_id, recipient_id, call_time
FROM Calls
UNION ALL
SELECT recipient_id caller_id, caller_id recipient_id, call_time
FROM Calls
)

SELECT DISTINCT a.caller_id user_id
FROM
(SELECT caller_id, recipient_id, dense_rank() over (PARTITION BY caller_id, DATE_FORMAT(call_time, '%Y-%m-%d') order by call_time) AS rk
FROM a) a
INNER JOIN
(SELECT caller_id, recipient_id, dense_rank() over (PARTITION BY caller_id, DATE_FORMAT(call_time, '%Y-%m-%d') order by call_time DESC) AS rk
FROM a) b
ON a.caller_id = b.caller_id AND a.recipient_id = b.recipient_id AND a.rk = 1 AND b.rk = 1


举报

相关推荐

0 条评论