RETENTION

SinceVersion 1.2.0

RETENTION

Description

Syntax

retention(event1, event2, ... , eventN);

The retention function takes as arguments a set of conditions from 1 to 32 arguments of type UInt8 that indicate whether a certain condition was met for the event. Any condition can be specified as an argument.

The conditions, except the first, apply in pairs: the result of the second will be true if the first and second are true, of the third if the first and third are true, etc.

Arguments

event — An expression that returns a UInt8 result (1 or 0).

Returned value

The array of 1 or 0.

1 — Condition was met for the event.

0 — Condition wasn’t met for the event.

example

  1. DROP TABLE IF EXISTS retention_test;
  2. CREATE TABLE retention_test(
  3. `uid` int COMMENT 'user id',
  4. `date` datetime COMMENT 'date time'
  5. )
  6. DUPLICATE KEY(uid)
  7. DISTRIBUTED BY HASH(uid) BUCKETS 3
  8. PROPERTIES (
  9. "replication_num" = "1"
  10. );
  11. INSERT into retention_test (uid, date) values (0, '2022-10-12'),
  12. (0, '2022-10-13'),
  13. (0, '2022-10-14'),
  14. (1, '2022-10-12'),
  15. (1, '2022-10-13'),
  16. (2, '2022-10-12');
  17. SELECT * from retention_test;
  18. +------+---------------------+
  19. | uid | date |
  20. +------+---------------------+
  21. | 0 | 2022-10-14 00:00:00 |
  22. | 0 | 2022-10-13 00:00:00 |
  23. | 0 | 2022-10-12 00:00:00 |
  24. | 1 | 2022-10-13 00:00:00 |
  25. | 1 | 2022-10-12 00:00:00 |
  26. | 2 | 2022-10-12 00:00:00 |
  27. +------+---------------------+
  28. SELECT
  29. uid,
  30. retention(date = '2022-10-12')
  31. AS r
  32. FROM retention_test
  33. GROUP BY uid
  34. ORDER BY uid ASC;
  35. +------+------+
  36. | uid | r |
  37. +------+------+
  38. | 0 | [1] |
  39. | 1 | [1] |
  40. | 2 | [1] |
  41. +------+------+
  42. SELECT
  43. uid,
  44. retention(date = '2022-10-12', date = '2022-10-13')
  45. AS r
  46. FROM retention_test
  47. GROUP BY uid
  48. ORDER BY uid ASC;
  49. +------+--------+
  50. | uid | r |
  51. +------+--------+
  52. | 0 | [1, 1] |
  53. | 1 | [1, 1] |
  54. | 2 | [1, 0] |
  55. +------+--------+
  56. SELECT
  57. uid,
  58. retention(date = '2022-10-12', date = '2022-10-13', date = '2022-10-14')
  59. AS r
  60. FROM retention_test
  61. GROUP BY uid
  62. ORDER BY uid ASC;
  63. +------+-----------+
  64. | uid | r |
  65. +------+-----------+
  66. | 0 | [1, 1, 1] |
  67. | 1 | [1, 1, 0] |
  68. | 2 | [1, 0, 0] |
  69. +------+-----------+

keywords

RETENTION