LeetCode MySQL 1633. 各赛事的用户注册率
生活随笔
收集整理的這篇文章主要介紹了
LeetCode MySQL 1633. 各赛事的用户注册率
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
用戶表: Users
+-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+user_id 是該表的主鍵。
該表中的每行包括用戶 ID 和用戶名。
注冊表: Register
+-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+(contest_id, user_id) 是該表的主鍵。
該表中的每行包含用戶的 ID 和他們注冊的賽事。
寫一條 SQL 語句,查詢各賽事的用戶注冊百分率,保留兩位小數。
返回的結果表按 percentage 的降序排序,若相同則按 contest_id 的升序排序。
查詢結果如下示例所示:
Users 表:
+---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+Register 表:
+------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+結果表:
+------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 所有用戶都注冊了 208、209 和 210 賽事,因此這些賽事的注冊率為 100% , 我們按 contest_id 的降序排序加入結果表中。 Alice 和 Alex 注冊了 215 賽事,注冊率為 ((2/3) * 100) = 66.67% Bob 注冊了 207 賽事,注冊率為 ((1/3) * 100) = 33.33%來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/percentage-of-users-attended-a-contest
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
# Write your MySQL query statement below select contest_id, round(ct/tot*100,2) percentage from (select contest_id, count(*) ctfrom Register rgroup by contest_id )t1, (select count(*) tot from Users) t2 order by percentage desc, contest_id asc959 ms 0 B MySQL
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode MySQL 1633. 各赛事的用户注册率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 2169. 得到 0
- 下一篇: LeetCode MySQL 1581.