返回介绍

solution / 2200-2299 / 2205.The Number of Users That Are Eligible for Discount / README_EN

发布于 2024-06-17 01:03:08 字数 2767 浏览 0 评论 0 收藏 0

2205. The Number of Users That Are Eligible for Discount

中文文档

Description

Table: Purchases

+-------------+----------+
| Column Name | Type   |
+-------------+----------+
| user_id   | int    |
| time_stamp  | datetime |
| amount    | int    |
+-------------+----------+
(user_id, time_stamp) is the primary key (combination of columns with unique values) 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 a solution to report the number of users that are eligible for a discount.

The 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.

Solutions

Solution 1

CREATE FUNCTION getUserIDs(startDate DATE, endDate DATE, minAmount INT) RETURNS INT
BEGIN
  RETURN (
    SELECT COUNT(DISTINCT user_id) AS user_cnt
    FROM Purchases
    WHERE time_stamp BETWEEN startDate AND endDate AND amount >= minAmount
  );
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文