1141.查询近30天活跃用户数
🚀 活动记录表:Activity +---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum | +---------------+---------+ 该表是用户在社交网站的活动记录。 该表没有主键,可能包含重复数据。 activity_type 字段为以下四种值 ('open_session', 'end_session', 'scroll_down', 'send_message')。 每个 session_id 只属于一个用户。 🚀 需求 请写SQL查询出截至 2019-07-27(包含2019-07-27),近 30 天的每日活跃用户数(当天只要有一条活动记录,即为活跃用户)。 以 任意顺序 返回结果表。 查询结果示例如下。 示例 1: 输入: Activity table: +---------+------------+---------------+---------------+ | user_id | session_id | activity_date | activity_type | +---------+------------+---------------+---------------+ | 1 | 1 | 2019-07-20 | open_session | | 1 | 1 | 2019-07-20 | scroll_down | | 1 | 1 | 2019-07-20 | end_session | | 2 | 4 | 2019-07-20 | open_session | | 2 | 4 | 2019-07-21 | send_message | | 2 | 4 | 2019-07-21 | end_session | | 3 | 2 | 2019-07-21 | open_session | | 3 | 2 | 2019-07-21 | send_message | | 3 | 2 | 2019-07-21 | end_session | | 4 | 3 | 2019-06-25 | open_session | | 4 | 3 | 2019-06-25 | end_session | +---------+------------+---------------+---------------+ 输出: +------------+--------------+ | day | active_users | +------------+--------------+ | 2019-07-20 | 2 | | 2019-07-21 | 2 | +------------+--------------+ 解释:注意非活跃用户的记录不需要展示。 🐴🐴 答案 # Write your MySQL query statement below select activity_date day, count(distinct user_id) active_users from activity where activity_date > date_sub('2019-07-27', interval 30 day) and activity_date <= '2019-07-27' group by activity_date /* Write your T-SQL query statement below */ select activity_date day, count(distinct user_id ) active_users from Activity where activity_date > CONVERT(varchar(100), dateadd(dd,-30,'2019-07-27'), 23) and activity_date<='2019-07-27' group by activity_date /* Write your PL/SQL query statement below */ select to_char(activity_date,'yyyy-mm-dd') "day", count(distinct user_id ) "active_users" from Activity where activity_date > to_date('20190727','yyyymmdd')-30 and to_char(activity_date,'yyyy-mm-dd') < '2019-07-27' group by to_char(activity_date,'yyyy-mm-dd')
1693.每天的领导和合伙人
🚀 表:DailySales +-------------+---------+ | Column Name | Type | +-------------+---------+ | date_id | date | | make_name | varchar | | lead_id | int | | partner_id | int | +-------------+---------+ 该表没有主键。 该表包含日期、产品的名称,以及售给的领导和合伙人的编号。 名称只包含小写英文字母。 🚀 需求 写一条 SQL 语句,使得对于每一个 date_id 和 make_name,返回不同的 lead_id 以及不同的 partner_id 的数量。 按 任意顺序 返回结果表。 查询结果格式如下示例所示。 示例 1: 输入: DailySales 表: +-----------+-----------+---------+------------+ | date_id | make_name | lead_id | partner_id | +-----------+-----------+---------+------------+ | 2020-12-8 | toyota | 0 | 1 | | 2020-12-8 | toyota | 1 | 0 | | 2020-12-8 | toyota | 1 | 2 | | 2020-12-7 | toyota | 0 | 2 | | 2020-12-7 | toyota | 0 | 1 | | 2020-12-8 | honda | 1 | 2 | | 2020-12-8 | honda | 2 | 1 | | 2020-12-7 | honda | 0 | 1 | | 2020-12-7 | honda | 1 | 2 | | 2020-12-7 | honda | 2 | 1 | +-----------+-----------+---------+------------+ 输出: +-----------+-----------+--------------+-----------------+ | date_id | make_name | unique_leads | unique_partners | +-----------+-----------+--------------+-----------------+ | 2020-12-8 | toyota | 2 | 3 | | 2020-12-7 | toyota | 1 | 2 | | 2020-12-8 | honda | 2 | 2 | | 2020-12-7 | honda | 3 | 2 | +-----------+-----------+--------------+-----------------+ 解释: 在 2020-12-8,丰田(toyota)有领导者 = [0, 1] 和合伙人 = [0, 1, 2] ,同时本田(honda)有领导者 = [1, 2] 和合伙人 = [1, 2]。 在 2020-12-7,丰田(toyota)有领导者 = [0] 和合伙人 = [1, 2] ,同时本田(honda)有领导者 = [0, 1, 2] 和合伙人 = [1, 2]。 🐴🐴 答案 # Write your MySQL query statement below select date_id,make_name, count(distinct lead_id) unique_leads, count(distinct partner_id) unique_partners from DailySales group by date_id,make_name /* Write your PL/SQL query statement below */ select to_char(date_id,'yyyy-mm-dd') "date_id", make_name "make_name", count(distinct lead_id) "unique_leads", count(distinct partner_id) "unique_partners" from DailySales group by to_char(date_id,'yyyy-mm-dd'),make_name
1729.求关注者的数量
🚀 表: Followers +-------------+------+ | Column Name | Type | +-------------+------+ | user_id | int | | follower_id | int | +-------------+------+ (user_id, follower_id) 是这个表的主键。 该表包含一个关注关系中关注者和用户的编号,其中关注者关注用户。 🚀 需求 写出 SQL 语句,对于每一个用户,返回该用户的关注者数量。 按 user_id 的顺序返回结果表。 查询结果的格式如下示例所示。 示例 1: 输入: Followers 表: +---------+-------------+ | user_id | follower_id | +---------+-------------+ | 0 | 1 | | 1 | 0 | | 2 | 0 | | 2 | 1 | +---------+-------------+ 输出: +---------+----------------+ | user_id | followers_count| +---------+----------------+ | 0 | 1 | | 1 | 1 | | 2 | 2 | +---------+----------------+ 解释: 0 的关注者有 {1} 1 的关注者有 {0} 2 的关注者有 {0,1} 🐴🐴 答案 # Write your MySQL query statement below SELECT user_id, COUNT(DISTINCT follower_id) followers_count FROM Followers GROUP BY user_id ORDER BY user_id; /* Write your T-SQL query statement below */ SELECT user_id, COUNT(DISTINCT follower_id) followers_count FROM Followers GROUP BY user_id ORDER BY user_id; /* Write your PL/SQL query statement below */ SELECT user_id "user_id", COUNT(DISTINCT follower_id) "followers_count" FROM Followers GROUP BY user_id ORDER BY user_id;