> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# retention

> 计算一段时间内用户的留存率，接收1到31个条件并返回由0和1组成的数组。

retention (留存函数）用于计算一段时间内的用户留存情况。该函数接收 1 到 31 个条件，从第一个条件开始判断事件是否满足条件，如果条件满足则输出 1，不满足则输出 0，最终返回 0 和 1 的数组。通过统计结果为 1 的数据，计算用户留存率。

## 语法

```Haskell theme={null}
retention(array)
```

## 参数说明

`array`：一系列条件表达式组成的数组，类型为 ARRAY。数组内最多支持传入 31 个条件，多个条件用逗号隔开。

## 返回值说明

返回包含 0 和 1 的数组。数组里 0 和 1 的个数与传入的条件数一致。

从数组里第一个条件开始依次判断：

* 如果事件满足当前条件，则输出 1。

* 如果事件不满足当前条件，则当前位置及之后的所有位置均为 0。

## 示例

1. 创建表 `test` 并插入数据。

   ```SQL theme={null}
   CREATE TABLE test(
       id TINYINT,
       action STRING,
       time DATETIME
   )
   ENGINE=olap
   DUPLICATE KEY(id)
   DISTRIBUTED BY HASH(id);

   INSERT INTO test VALUES 
   (1,'pv','2022-01-01 08:00:05'),
   (2,'pv','2022-01-01 10:20:08'),
   (1,'buy','2022-01-02 15:30:10'),
   (2,'pv','2022-01-02 17:30:05'),
   (3,'buy','2022-01-01 05:30:09'),
   (3,'buy','22022-01-02 08:10:15'),
   (4,'pv','2022-01-02 21:09:15'),
   (5,'pv','2022-01-01 22:10:53'),
   (5,'pv','2022-01-02 19:10:52'),
   (5,'buy','2022-01-02 20:00:50');
   ```

2. 查询 `test` 表中数据。

   ```Plain Text theme={null}
   MySQL > select * from test order by id;
   +------+--------+---------------------+
   | id   | action | time                |
   +------+--------+---------------------+
   |    1 | pv     | 2022-01-01 08:00:05 |
   |    1 | buy    | 2022-01-02 15:30:10 |
   |    2 | pv     | 2022-01-01 10:20:08 |
   |    2 | pv     | 2022-01-02 17:30:05 |
   |    3 | buy    | 2022-01-01 05:30:09 |
   |    3 | buy    | 2022-01-02 08:10:15 |
   |    4 | pv     | 2022-01-02 21:09:15 |
   |    5 | pv     | 2022-01-01 22:10:53 |
   |    5 | pv     | 2022-01-02 19:10:52 |
   |    5 | buy    | 2022-01-02 20:00:50 |
   +------+--------+---------------------+
   10 rows in set (0.01 sec)
   ```

3. 使用 `retention` 函数计算用户留存率。

   示例一：计算2022年1月1日浏览过商品页（action='pv'），并且在2022年1月2日下单（action='buy'）的情况。

   ```Plain Text theme={null}
   MySQL > select id, retention([action='pv' and to_date(time)='2022-01-01',
                                 action='buy' and to_date(time)='2022-01-02']) as retention 
   from test 
   group by id
   order by id;

   +------+-----------+
   | id   | retention |
   +------+-----------+
   |    1 | [1,1]     |
   |    2 | [1,0]     |
   |    3 | [0,0]     |
   |    4 | [0,0]     |
   |    5 | [1,1]     |
   +------+-----------+
   5 rows in set (0.01 sec)
   ```

   可以看到：

   * 用户1和用户5在两天内都满足指定条件，因此返回数组\[1,1]。

   * 用户2在第二天没有满足指定条件，因此返回数组\[1,0]。

   * 用户3即便在第二天满足了指定条件，但因为第一天没有满足，返回数组\[0,0]。

   * 用户4两天都未满足指定条件，因此返回数组\[0,0]。

   示例二：计算2022年1月1日浏览过商品页（action='pv'），并且在2022年1月2日下单（action='buy'）的用户比例。

   ```Plain Text theme={null}
   MySQL > select sum(r[1]),sum(r[2])/sum(r[1])
   from (select id, retention([action='pv' and to_date(time)='2022-01-01',
                               action='buy' and to_date(time)='2022-01-02']) as r 
   from test 
   group by id 
   order by id) t;

   +-----------+---------------------------+
   | sum(r[1]) | (sum(r[2])) / (sum(r[1])) |
   +-----------+---------------------------+
   |         3 |        0.6666666666666666 |
   +-----------+---------------------------+
   1 row in set (0.02 sec)
   ```

   返回结果即2022年1月2日的用户留存率。

## Keywords

retention，留存，留存率


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.