• 2205. The Number of Users That Are Eligible for Discount0


    SQL架构

    Table: Purchases

    +-------------+----------+
    | Column Name | Type     |
    +-------------+----------+
    | user_id     | int      |
    | time_stamp  | datetime |
    | amount      | int      |
    +-------------+----------+
    (user_id, time_stamp) is the primary key for this table.
    Each row contains information about the purchase time and the amount paid for the user with ID user_id.
    

    A user is eligible for a discount if they had a purchase in the inclusive interval of time [startDate, endDate] with at least minAmount amount. To convert the dates to times, both dates should be considered as the start of the day (i.e., endDate = 2022-03-05 should be considered as the time 2022-03-05 00:00:00).

    Write an SQL query to report the number of users that are eligible for a discount.

    The query result format is in the following example.

    Example 1:

    Input: 
    Purchases table:
    +---------+---------------------+--------+
    | user_id | time_stamp          | amount |
    +---------+---------------------+--------+
    | 1       | 2022-04-20 09:03:00 | 4416   |
    | 2       | 2022-03-19 19:24:02 | 678    |
    | 3       | 2022-03-18 12:03:09 | 4523   |
    | 3       | 2022-03-30 09:43:42 | 626    |
    +---------+---------------------+--------+
    startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000
    Output: 
    +----------+
    | user_cnt |
    +----------+
    | 1        |
    +----------+
    Explanation:
    Out of the three users, only User 3 is eligible for a discount.
     - User 1 had one purchase with at least minAmount amount, but not within the time interval.
     - User 2 had one purchase within the time interval, but with less than minAmount amount.
     - User 3 is the only user who had a purchase that satisfies both conditions.
    1. CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
    2. BEGIN
    3. RETURN (
    4. # Write your MySQL query statement below.
    5. select
    6. count(distinct user_id)
    7. from
    8. Purchases
    9. where amount >=minAmount and
    10. time_stamp >= date_format(concat(startDate,' 00:00:00'),'%Y-%m-%d %H:%i:%s') and time_stamp <= date_format(concat(endDate,' 00:00:00'),'%Y-%m-%d %H:%i:%s') and amount >=minAmount
    11. );
    12. END
    1. CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
    2. BEGIN
    3. RETURN (
    4. # Write your MySQL query statement below.
    5. select
    6. count(distinct user_id)
    7. from
    8. Purchases
    9. where amount >=minAmount and
    10. time_stamp >= concat(startDate,' 00:00:00')and time_stamp <=concat(endDate,' 00:00:00')and amount >=minAmount
    11. );
    12. END

  • 相关阅读:
    Kotlin编程实战——协程(08)
    【python与数据分析】Pandas统计分析基础
    Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别
    SpringBoot Windows 自启动 - 通过 Windows Service 服务实现
    OnlineJudge平台(负载均衡)
    openssl做文件处理(base64,MD5,sha256等)
    redis缓存一致性讨论
    高效的工作学习方法
    k8s删除node
    int 0x13 读取磁盘到内存 Loading Sectors
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125633658