美文网首页
Leetcode1454. 活跃用户 (中等)

Leetcode1454. 活跃用户 (中等)

作者: kaka22 | 来源:发表于2020-07-24 10:15 被阅读0次

    题目
    Table Accounts:

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | id            | int     |
    | name          | varchar |
    +---------------+---------+
    

    the id is the primary key for this table.
    This table contains the account id and the user name of each account.

    Table Logins:

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | id            | int     |
    | login_date    | date    |
    +---------------+---------+
    

    There is no primary key for this table, it may contain duplicates.
    This table contains the account id of the user who logged in and the login date. A user may log in multiple times in the day.

    Write an SQL query to find the id and the name of active users.

    Active users are those who logged in to their accounts for 5 or more consecutive days.

    Return the result table ordered by the id.

    The query result format is in the following example:

    Accounts table:

    +----+----------+
    | id | name     |
    +----+----------+
    | 1  | Winston  |
    | 7  | Jonathan |
    +----+----------+
    

    Logins table:

    +----+------------+
    | id | login_date |
    +----+------------+
    | 7  | 2020-05-30 |
    | 1  | 2020-05-30 |
    | 7  | 2020-05-31 |
    | 7  | 2020-06-01 |
    | 7  | 2020-06-02 |
    | 7  | 2020-06-02 |
    | 7  | 2020-06-03 |
    | 1  | 2020-06-07 |
    | 7  | 2020-06-10 |
    +----+------------+
    

    Result table:

    +----+----------+
    | id | name     |
    +----+----------+
    | 7  | Jonathan |
    +----+----------+
    

    User Winston with id = 1 logged in 2 times only in 2 different days, so, Winston is not an active user.
    User Jonathan with id = 7 logged in 7 times in 6 different days, five of them were consecutive days, so, Jonathan is an active user.

    解答
    选出不同用户不同天数的登陆次数超过五天的用户

    select L.id
    from  Logins as L
    group by L.id
    having count(distinct L.login_date) >=5
    

    选出对应的姓名

    select A.id, A.name
    from Accounts  as A
    where A.id in (select L.id
    from  Logins as L
    group by L.id
    having count(distinct L.login_date) >=5)
    

    相关文章

      网友评论

          本文标题:Leetcode1454. 活跃用户 (中等)

          本文链接:https://www.haomeiwen.com/subject/pmtjlktx.html