力扣sql中等篇练习(十八)
1 银行账户概要
1.1 题目内容
1.1.1 基本题目信息1
1.1.2 基本题目信息2
1.1.3 示例输入输出
1.2 示例sql语句
SELECT u.user_id,u.user_name,u.credit+IFNULL(t1.c1,0) credit,
case when u.credit+IFNULL(t1.c1,0)<0 then 'Yes'
else 'No'
end credit_limit_breached
FROM Users u
LEFT JOIN
(
SELECT t.user_id,sum(t.credit) c1
FROM
(
SELECT paid_by user_id,amount*-1 credit
FROM Transactions
UNION ALL
SELECT paid_to user_id,amount credit
FROM Transactions
)t
GROUP BY t.user_id
)t1
ON u.user_id=t1.user_id
1.3 运行截图
2 找到遗失的ID
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
WITH RECURSIVE t1(n) AS
(
SELECT 1
UNION ALL
SELECT n+1 FROM t1 WHERE n<(SELECT max(customer_id) FROM Customers)
)
SELECT t1.n ids
FROM t1
WHERE t1.n NOT IN
(
SELECT customer_id
FROM Customers
)
ORDER BY ids asc
2.3 运行截图
3 访问日期之间最大的空档期
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
SELECT t4.user_id,max(t4.mGap) biggest_window
FROM
(
SELECT t3.user_id,t3.t1,min(t3.gap) mGap
FROM
(
SELECT u1.user_id,u1.visit_date t1,ifnull(u2.visit_date,'2021-1-1') t2,
datediff(ifnull(u2.visit_date,'2021-1-1'),u1.visit_date) gap
FROM UserVisits u1
LEFT JOIN UserVisits u2
ON u1.user_id=u2.user_id AND u1.visit_date<u2.visit_date
)t3
GROUP BY t3.user_id,t3.t1
)t4
GROUP BY t4.user_id
ORDER BY t4.user_id asc
3.3 运行截图