力扣sql中等篇练习(十八)
1 银行账户概要
1.1 题目内容
1.1.1 基本题目信息1
1.1.2 基本题目信息2
1.1.3 示例输入输出
1.2 示例sql语句
# Write your MySQL query statement below
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语句
# 主要是求1到最后一个数之间的临时表,主要临界条件
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 示例输入输出
文章来源:https://www.toymoban.com/news/detail-438477.html
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 运行截图
文章来源地址https://www.toymoban.com/news/detail-438477.html
到了这里,关于力扣sql中等篇练习(十八)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!