首页 > 试题广场 >

SaaS平台企业客户新功能采纳度分析

[编程题]SaaS平台企业客户新功能采纳度分析
  • 热度指数:126 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

背景

一家SaaS公司发布了一项名为“高级分析仪表盘”('Advanced_Analytics')的新功能,该功能仅对“企业版”('Enterprise')套餐的客户开放。为了评估新功能的初期采纳情况,产品团队需要识别出在功能发布首月(2025年4月)内,哪些企业版客户团队开始使用这项新功能,并分析他们的使用强度。

表结构和字段说明

表1:客户团队表 (teams)

  • team_id: (INT, 主键) 团队的唯一标识。
  • team_name: (VARCHAR) 团队的名称。
  • plan_level: (VARCHAR) 团队订阅的套餐级别,如 'Free', 'Pro', 'Enterprise'。
  • creation_date: (DATE) 团队账户的创建日期。

表2:功能使用日志表 (feature_usage)

  • usage_id: (INT, 主键) 使用记录的唯一标识。
  • team_id: (INT) 使用功能的团队ID。
  • feature_name: (VARCHAR) 所使用功能的名称,如 'SSO', 'API_Access', 'Advanced_Analytics'。
  • usage_timestamp: (DATETIME) 功能被使用的精确时间戳。

问题

任务要求

请查询所有在 2025年4月 期间(即 2025-04-01 至 2025-04-30)使用了 'Advanced_Analytics' 功能的 'Enterprise' 套餐团队。对于这些团队,请计算他们在该月内使用此功能的总次数,并找出他们首次使用我们平台任何功能的最早日期。根据总使用次数,将团队分为两类:总次数大于50次的为“深度采纳团队”,否则为“普通采纳团队”。

查询结果要求

请返回以下字段:

  • 团队ID (team_id)
  • 团队名称 (team_name)
  • 4月总使用次数 (april_usage_count)
  • 采纳类型 (adoption_category)
  • 平台首次使用日期 (first_ever_usage_date),格式为 'YYYY-MM-DD'。

排序规则

查询结果请先按adoption_category降序排列(即 "深度采纳团队" 在前),然后按april_usage_count降序排列,最后按team_id升序排列。

示例1

输入

DROP TABLE IF EXISTS feature_usage;
DROP TABLE IF EXISTS teams;

-- 创建客户团队表
CREATE TABLE teams (
    team_id INT,
    team_name VARCHAR(50),
    plan_level VARCHAR(20),
    creation_date DATE
);

-- 创建功能使用日志表
CREATE TABLE feature_usage (
    usage_id INT,
    team_id INT,
    feature_name VARCHAR(50),
    usage_timestamp DATETIME
);

-- --------------------------------------------------------
-- 2. Data Insertion (DML)
-- --------------------------------------------------------

-- 客户团队
INSERT INTO teams (team_id, team_name, plan_level, creation_date) VALUES
(1, 'Innovate Corp', 'Enterprise', '2025-01-10'),
(2, 'Data Driven Inc', 'Enterprise', '2025-02-20'),
(3, 'Agile Solutions', 'Pro', '2025-03-01'), -- 非企业版
(4, 'Legacy Systems', 'Enterprise', '2025-01-05');

-- 功能使用日志
INSERT INTO feature_usage (usage_id, team_id, feature_name, usage_timestamp) VALUES
(101, 1, 'SSO', '2025-02-15 09:00:00'), -- Innovate Corp 首次使用
(102, 1, 'Advanced_Analytics', '2025-04-02 10:00:00'), -- 4月使用
(103, 1, 'Advanced_Analytics', '2025-04-05 11:30:00'), -- 4月使用 (总计2次)
(104, 2, 'API_Access', '2025-03-01 14:00:00'), -- Data Driven Inc 首次使用
(105, 2, 'Advanced_Analytics', '2025-04-10 16:00:00'), -- 4月使用
(106, 2, 'Advanced_Analytics', '2025-05-01 10:00:00'), -- 5月使用 (不计入)
(107, 3, 'Advanced_Analytics', '2025-04-15 11:00:00'), -- Pro团队使用 (不计入)
(108, 4, 'Advanced_Analytics', '2025-03-28 17:00:00'); -- 3月使用 (不计入)

-- 为Data Driven Inc (team_id=2) 增加51条使用记录,使其4月总数达到52次
INSERT INTO feature_usage (usage_id, team_id, feature_name, usage_timestamp)
SELECT
    200 + v.seq, 2, 'Advanced_Analytics', '2025-04-12 10:00:00'
FROM (
    SELECT 1 as seq UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
    SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL
    SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL
    SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL
    SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL
    SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL SELECT 30 UNION ALL
    SELECT 31 UNION ALL SELECT 32 UNION ALL SELECT 33 UNION ALL SELECT 34 UNION ALL SELECT 35 UNION ALL
    SELECT 36 UNION ALL SELECT 37 UNION ALL SELECT 38 UNION ALL SELECT 39 UNION ALL SELECT 40 UNION ALL
    SELECT 41 UNION ALL SELECT 42 UNION ALL SELECT 43 UNION ALL SELECT 44 UNION ALL SELECT 45 UNION ALL
    SELECT 46 UNION ALL SELECT 47 UNION ALL SELECT 48 UNION ALL SELECT 49 UNION ALL SELECT 50 UNION ALL
    SELECT 51
) as v;

输出

team_id|team_name|april_usage_count|adoption_category|first_ever_usage_date
2|Data Driven Inc|52|深度采纳团队|2025-03-01
1|Innovate Corp|2|普通采纳团队|2025-02-15
-- 和上一题解法一样

-- 目标团队
with target_team as (
    select
        t.team_id
        ,t.team_name
        ,count(*) as april_usage_count
    from
        teams as t
        inner join feature_usage as f on t.team_id = f.team_id
    where 
        f.usage_timestamp >= '2025-04-01' and f.usage_timestamp <'2025-05-01'
        and t.plan_level = 'Enterprise'
        and f.feature_name = 'Advanced_Analytics'
    group by 
        t.team_id, t.team_name 
), 

-- 这些团队首次使用我们平台任何功能的最早日期
first_tb as (
    select 
        team_id
        ,date_format(min(usage_timestamp), '%Y-%m-%d') as first_ever_usage_date
    from 
        feature_usage
    where
        team_id in (select team_id from target_team)
    group by 
        team_id
)

-- 输出
select
    t.team_id
    ,t.team_name
    ,t.april_usage_count
    ,case when t.april_usage_count > 50 then '深度采纳团队' else '普通采纳团队' end as adoption_category
    ,f.first_ever_usage_date
from 
    target_team as t
    inner join first_tb as f on t.team_id = f.team_id
order by 
    case when adoption_category = '深度采纳团队' then 0 else 1 end asc, 
    t.april_usage_count desc ,
    t.team_id asc ;

发表于 2026-02-03 21:21:28 回复(1)
select team_id,team_name,
        sum(case when usage_timestamp like '2025-04%' then 1 else 0 end) april_usage_count,
        case when (sum(case when usage_timestamp like '2025-04%' then 1 else 0 end))>50 then '深度采纳团队' else '普通采纳团队' end adoption_category,
        first_ever_usage_date

from(
    select team_id,team_name,usage_timestamp,
        date(min(usage_timestamp)over(partition by team_id))first_ever_usage_date
    from feature_usage
    join teams using(team_id)
    where team_id in (
        select team_id 
        from feature_usage
        join teams using(team_id)
        where usage_timestamp like '2025-04%'
        and feature_name = 'Advanced_Analytics'
        and plan_level = 'Enterprise'
    )
)a
group by team_id,team_name,first_ever_usage_date
order by adoption_category desc,april_usage_count desc,team_id

发表于 2026-02-03 10:34:07 回复(0)
with tm as(
    select distinct team_id
    from teams
    where plan_level='Enterprise'
),
cnt as(
    select team_id,ifnull(count(*),0) as april_usage_count
    from feature_usage fu
    where team_id in (select team_id from tm)
      and feature_name='Advanced_Analytics'
      and date(usage_timestamp) between '2025-04-01' and '2025-04-30'
    group by team_id
)

select distinct cnt.team_id,team_name,
       april_usage_count,
       case when april_usage_count>50 then '深度采纳团队' else '普通采纳团队' end as adoption_category,
       date(m_time) as first_ever_usage_date
from (select team_id,min(usage_timestamp) over(partition by team_id)  as m_time
      from feature_usage) fu
inner join cnt on fu.team_id=cnt.team_id
inner join teams on fu.team_id = teams.team_id
where fu.team_id in (select team_id from tm)
order by field(adoption_category,'深度采纳团队','普通采纳团队'),april_usage_count desc ,team_id asc

发表于 2026-02-01 20:28:38 回复(0)
select team_id,team_name,
        sum(case when usage_timestamp like '2025-04%' then 1 else 0 end) april_usage_count,
        case when (sum(case when usage_timestamp like '2025-04%' then 1 else 0 end))>50 then '深度采纳团队' else '普通采纳团队' end adoption_category,
        first_ever_usage_date

from(
    select team_id,team_name,usage_timestamp,
        date(min(usage_timestamp)over(partition by team_id))first_ever_usage_date
    from feature_usage
    join teams using(team_id)
    where team_id in (
        select team_id 
        from feature_usage
        join teams using(team_id)
        where usage_timestamp like '2025-04%'
        and feature_name = 'Advanced_Analytics'
        and plan_level = 'Enterprise'
    )
)a
group by team_id,team_name,first_ever_usage_date
order by adoption_category desc,april_usage_count desc,team_id

发表于 2026-02-01 13:26:23 回复(0)