RETENTION

SinceVersion 1.2.0

RETENTION

description

Syntax

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

留存函数将一组条件作为参数,类型为1到32个UInt8类型的参数,用来表示事件是否满足特定条件。 任何条件都可以指定为参数.

除了第一个以外,条件成对适用:如果第一个和第二个是真的,第二个结果将是真的,如果第一个和第三个是真的,第三个结果将是真的,等等。

Arguments

event — 返回UInt8结果(1或0)的表达式.

Returned value

由1和0组成的数组。

1 — 条件满足。

0 — 条件不满足

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