running_difference

description

Syntax

T running_difference(T x) 计算数据块中连续行值的差值。该函数的结果取决于受影响的数据块和块中数据的顺序。

计算 running_difference 期间使用的行顺序可能与返回给用户的行顺序不同。所以结果是不稳定的。此函数会在后续版本中废弃。 推荐使用窗口函数完成预期功能。举例如下:

  1. -- running difference(x)
  2. SELECT running_difference(x) FROM t ORDER BY k;
  3. -- 窗口函数
  4. SELECT x - lag(x, 1, 0) OVER (ORDER BY k) FROM t;

Arguments

x - 一列数据.数据类型可以是TINYINT,SMALLINT,INT,BIGINT,LARGEINT,FLOAT,DOUBLE,DATE,DATETIME,DECIMAL

Returned value

第一行返回 0,随后的每一行返回与前一行的差值。

example

  1. DROP TABLE IF EXISTS running_difference_test;
  2. CREATE TABLE running_difference_test (
  3. `id` int NOT NULL COMMENT 'id',
  4. `day` date COMMENT 'day',
  5. `time_val` datetime COMMENT 'time_val',
  6. `doublenum` double NULL COMMENT 'doublenum'
  7. )
  8. DUPLICATE KEY(id)
  9. DISTRIBUTED BY HASH(id) BUCKETS 3
  10. PROPERTIES (
  11. "replication_num" = "1"
  12. );
  13. INSERT into running_difference_test (id, day, time_val,doublenum) values ('1', '2022-10-28', '2022-03-12 10:41:00', null),
  14. ('2','2022-10-27', '2022-03-12 10:41:02', 2.6),
  15. ('3','2022-10-28', '2022-03-12 10:41:03', 2.5),
  16. ('4','2022-9-29', '2022-03-12 10:41:03', null),
  17. ('5','2022-10-31', '2022-03-12 10:42:01', 3.3),
  18. ('6', '2022-11-08', '2022-03-12 11:05:04', 4.7);
  19. SELECT * from running_difference_test ORDER BY id ASC;
  20. +------+------------+---------------------+-----------+
  21. | id | day | time_val | doublenum |
  22. +------+------------+---------------------+-----------+
  23. | 1 | 2022-10-28 | 2022-03-12 10:41:00 | NULL |
  24. | 2 | 2022-10-27 | 2022-03-12 10:41:02 | 2.6 |
  25. | 3 | 2022-10-28 | 2022-03-12 10:41:03 | 2.5 |
  26. | 4 | 2022-09-29 | 2022-03-12 10:41:03 | NULL |
  27. | 5 | 2022-10-31 | 2022-03-12 10:42:01 | 3.3 |
  28. | 6 | 2022-11-08 | 2022-03-12 11:05:04 | 4.7 |
  29. +------+------------+---------------------+-----------+
  30. SELECT
  31. id,
  32. running_difference(id) AS delta
  33. FROM
  34. (
  35. SELECT
  36. id,
  37. day,
  38. time_val,
  39. doublenum
  40. FROM running_difference_test
  41. )as runningDifference ORDER BY id ASC;
  42. +------+-------+
  43. | id | delta |
  44. +------+-------+
  45. | 1 | 0 |
  46. | 2 | 1 |
  47. | 3 | 1 |
  48. | 4 | 1 |
  49. | 5 | 1 |
  50. | 6 | 1 |
  51. +------+-------+
  52. SELECT
  53. day,
  54. running_difference(day) AS delta
  55. FROM
  56. (
  57. SELECT
  58. id,
  59. day,
  60. time_val,
  61. doublenum
  62. FROM running_difference_test
  63. )as runningDifference ORDER BY id ASC;
  64. +------------+-------+
  65. | day | delta |
  66. +------------+-------+
  67. | 2022-10-28 | 0 |
  68. | 2022-10-27 | -1 |
  69. | 2022-10-28 | 1 |
  70. | 2022-09-29 | -29 |
  71. | 2022-10-31 | 32 |
  72. | 2022-11-08 | 8 |
  73. +------------+-------+
  74. SELECT
  75. time_val,
  76. running_difference(time_val) AS delta
  77. FROM
  78. (
  79. SELECT
  80. id,
  81. day,
  82. time_val,
  83. doublenum
  84. FROM running_difference_test
  85. )as runningDifference ORDER BY id ASC;
  86. +---------------------+-------+
  87. | time_val | delta |
  88. +---------------------+-------+
  89. | 2022-03-12 10:41:00 | 0 |
  90. | 2022-03-12 10:41:02 | 2 |
  91. | 2022-03-12 10:41:03 | 1 |
  92. | 2022-03-12 10:41:03 | 0 |
  93. | 2022-03-12 10:42:01 | 58 |
  94. | 2022-03-12 11:05:04 | 1383 |
  95. +---------------------+-------+
  96. SELECT
  97. doublenum,
  98. running_difference(doublenum) AS delta
  99. FROM
  100. (
  101. SELECT
  102. id,
  103. day,
  104. time_val,
  105. doublenum
  106. FROM running_difference_test
  107. )as runningDifference ORDER BY id ASC;
  108. +-----------+----------------------+
  109. | doublenum | delta |
  110. +-----------+----------------------+
  111. | NULL | NULL |
  112. | 2.6 | NULL |
  113. | 2.5 | -0.10000000000000009 |
  114. | NULL | NULL |
  115. | 3.3 | NULL |
  116. | 4.7 | 1.4000000000000004 |
  117. +-----------+----------------------+

keywords

running_difference