• 2051. The Category of Each Member in the Store


    SQL架构

    Table: Members

    +-------------+---------+
    | Column Name | Type    |
    +-------------+---------+
    | member_id   | int     |
    | name        | varchar |
    +-------------+---------+
    member_id is the primary key column for this table.
    Each row of this table indicates the name and the ID of a member.
    

    Table: Visits

    +-------------+------+
    | Column Name | Type |
    +-------------+------+
    | visit_id    | int  |
    | member_id   | int  |
    | visit_date  | date |
    +-------------+------+
    visit_id is the primary key column for this table.
    member_id is a foreign key to member_id from the Members table.
    Each row of this table contains information about the date of a visit to the store and the member who visited it.
    

    Table: Purchases

    +----------------+------+
    | Column Name    | Type |
    +----------------+------+
    | visit_id       | int  |
    | charged_amount | int  |
    +----------------+------+
    visit_id is the primary key column for this table.
    visit_id is a foreign key to visit_id from the Visits table.
    Each row of this table contains information about the amount charged in a visit to the store.
    

    A store wants to categorize its members. There are three tiers:

    • "Diamond": if the conversion rate is greater than or equal to 80.
    • "Gold": if the conversion rate is greater than or equal to 50 and less than 80.
    • "Silver": if the conversion rate is less than 50.
    • "Bronze": if the member never visited the store.

    The conversion rate of a member is (100 * total number of purchases for the member) / total number of visits for the member.

    Write an SQL query to report the id, the name, and the category of each member.

    Return the result table in any order.

    The query result format is in the following example.

    Example 1:

    Input: 
    Members table:
    +-----------+---------+
    | member_id | name    |
    +-----------+---------+
    | 9         | Alice   |
    | 11        | Bob     |
    | 3         | Winston |
    | 8         | Hercy   |
    | 1         | Narihan |
    +-----------+---------+
    Visits table:
    +----------+-----------+------------+
    | visit_id | member_id | visit_date |
    +----------+-----------+------------+
    | 22       | 11        | 2021-10-28 |
    | 16       | 11        | 2021-01-12 |
    | 18       | 9         | 2021-12-10 |
    | 19       | 3         | 2021-10-19 |
    | 12       | 11        | 2021-03-01 |
    | 17       | 8         | 2021-05-07 |
    | 21       | 9         | 2021-05-12 |
    +----------+-----------+------------+
    Purchases table:
    +----------+----------------+
    | visit_id | charged_amount |
    +----------+----------------+
    | 12       | 2000           |
    | 18       | 9000           |
    | 17       | 7000           |
    +----------+----------------+
    Output: 
    +-----------+---------+----------+
    | member_id | name    | category |
    +-----------+---------+----------+
    | 1         | Narihan | Bronze   |
    | 3         | Winston | Silver   |
    | 8         | Hercy   | Diamond  |
    | 9         | Alice   | Gold     |
    | 11        | Bob     | Silver   |
    +-----------+---------+----------+
    Explanation: 
    - User Narihan with id = 1 did not make any visits to the store. She gets a Bronze category.
    - User Winston with id = 3 visited the store one time and did not purchase anything. The conversion rate = (100 * 0) / 1 = 0. He gets a Silver category.
    - User Hercy with id = 8 visited the store one time and purchased one time. The conversion rate = (100 * 1) / 1 = 1. He gets a Diamond category.
    - User Alice with id = 9 visited the store two times and purchased one time. The conversion rate = (100 * 1) / 2 = 50. She gets a Gold category.
    - User Bob with id = 11 visited the store three times and purchased one time. The conversion rate = (100 * 1) / 3 = 33.33. He gets a Silver category.
    1. with t1 as (select
    2. member_id,count(p.visit_id)/count(visit_id) charged_amount#每个会员来店的消费次数/来店的次数
    3. from
    4. Visits v left join Purchases p
    5. using(visit_id)
    6. group by
    7. member_id
    8. )
    9. select
    10. # 用 if嵌套 找到 category of each member.
    11. m.member_id,m.name,if(100*t1.charged_amount>=80,'Diamond',if(100*t1.charged_amount<=80 and 100*t1.charged_amount>=50,'Gold',if(100*t1.charged_amount<50 and 100*t1.charged_amount>=0,'Silver','Bronze'))) category
    12. from
    13. Members m left join t1
    14. using(member_id)
    15. order by member_id

  • 相关阅读:
    C++跨DLL内存所有权问题探幽(二)CRT中MT和MD混用导致的所有权问题
    Meilisearch客户端完美改造
    怎样让健康码截图合并一张图片_健康码拼图
    面向OLAP的列式存储DBMS-1-[ClickHouse]的发展历程
    提高网申通过率的秘籍,校园招聘之春招秋招都有效
    前端,样式,行间距,字间距
    uniapp AES加密解密
    最全MacBook选购指南 | 看完你就知道怎么买
    阿里云+宝塔部署项目(Java+React)
    RabbitMQ 服务启动失败问题小结(Windows环境)
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125619040