SQL - SQL Server 2019:将间隔小于 14 天的行分组,并且 return 每组的第 1 行
SQL - SQL Server 2019: Group rows with that are less than 14 days apart and return the 1 row of each group
我有一张这样的桌子:
如果 t1_view_dt 彼此相隔 14 天以内,我需要在名为 t2_view_dt 的另一列中显示每组的第一行。
+------------+------+------------+------------+--------+
| row number | user | t1_view_dt | t2_view_dt | series |
+------------+------+------------+------------+--------+
| 1 | 123 | 1/1 | 1/1 | 1 |
+------------+------+------------+------------+--------+
| 2 | 123 | 1/10 | 1/10 | 1 |
+------------+------+------------+------------+--------+
| 3 | 123 | 1/20 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 4 | 123 | 1/25 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 5 | 123 | 1/30 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 6 | 123 | 2/10 | 2/10 | 3 |
+------------+------+------------+------------+--------+
| 7 | 123 | 2/15 | 2/10 | 3 |
+------------+------+------------+------------+--------+
例如,第 1 行和第 2 行属于同一组,因为组开始日期是 1/1,而 1/10 在从 1/1(第 1 行)开始的 14 天 window 内。日期为 1/20 的第 3 行在第 14 天之外 window,因此属于第 2 组(系列 2)等
我该怎么做?如何按 14 天划分日期以及如何根据此规则从一个新组开始?
您还没有提供 RDBMS。
这是 mySQL / mariaDB 的解决方案。
create table t1 (
user int,
view_dt date);
insert into t1 values
(122,'2022-01-02'),
(123,'2022-01-01'),
(123,'2022-01-10'),
(123,'2022-01-20'),
(123,'2022-01-25'),
(123,'2022-01-30'),
(123,'2022-02-10'),
(123,'2022-02-15');
SET @gNumber = 0;
SET @gDate = '1900-01-01';
set @user = 1;
SELECT
@gNumber := case
when @user = user and datediff(view_dt,@gDate) <= 14 then @gNumber
else @gNumber + 1 end groupNumber,
@user := user user,
view_dt,
@gDate := case when datediff(view_dt,@gDate) <= 14 then @gDate
else view_dt end groupDate
FROM t1
ORDER BY
user,
view_dt;
groupNumber | user | view_dt | groupDate
----------: | ---: | :--------- | :---------
1 | 122 | 2022-01-02 | 2022-01-02
2 | 123 | 2022-01-01 | 2022-01-02
2 | 123 | 2022-01-10 | 2022-01-02
3 | 123 | 2022-01-20 | 2022-01-20
3 | 123 | 2022-01-25 | 2022-01-20
3 | 123 | 2022-01-30 | 2022-01-20
4 | 123 | 2022-02-10 | 2022-02-10
4 | 123 | 2022-02-15 | 2022-02-10
db<>fiddle here
我有一张这样的桌子:
如果 t1_view_dt 彼此相隔 14 天以内,我需要在名为 t2_view_dt 的另一列中显示每组的第一行。
+------------+------+------------+------------+--------+
| row number | user | t1_view_dt | t2_view_dt | series |
+------------+------+------------+------------+--------+
| 1 | 123 | 1/1 | 1/1 | 1 |
+------------+------+------------+------------+--------+
| 2 | 123 | 1/10 | 1/10 | 1 |
+------------+------+------------+------------+--------+
| 3 | 123 | 1/20 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 4 | 123 | 1/25 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 5 | 123 | 1/30 | 1/20 | 2 |
+------------+------+------------+------------+--------+
| 6 | 123 | 2/10 | 2/10 | 3 |
+------------+------+------------+------------+--------+
| 7 | 123 | 2/15 | 2/10 | 3 |
+------------+------+------------+------------+--------+
例如,第 1 行和第 2 行属于同一组,因为组开始日期是 1/1,而 1/10 在从 1/1(第 1 行)开始的 14 天 window 内。日期为 1/20 的第 3 行在第 14 天之外 window,因此属于第 2 组(系列 2)等
我该怎么做?如何按 14 天划分日期以及如何根据此规则从一个新组开始?
您还没有提供 RDBMS。
这是 mySQL / mariaDB 的解决方案。
create table t1 ( user int, view_dt date); insert into t1 values (122,'2022-01-02'), (123,'2022-01-01'), (123,'2022-01-10'), (123,'2022-01-20'), (123,'2022-01-25'), (123,'2022-01-30'), (123,'2022-02-10'), (123,'2022-02-15');
SET @gNumber = 0; SET @gDate = '1900-01-01'; set @user = 1; SELECT @gNumber := case when @user = user and datediff(view_dt,@gDate) <= 14 then @gNumber else @gNumber + 1 end groupNumber, @user := user user, view_dt, @gDate := case when datediff(view_dt,@gDate) <= 14 then @gDate else view_dt end groupDate FROM t1 ORDER BY user, view_dt;
groupNumber | user | view_dt | groupDate ----------: | ---: | :--------- | :--------- 1 | 122 | 2022-01-02 | 2022-01-02 2 | 123 | 2022-01-01 | 2022-01-02 2 | 123 | 2022-01-10 | 2022-01-02 3 | 123 | 2022-01-20 | 2022-01-20 3 | 123 | 2022-01-25 | 2022-01-20 3 | 123 | 2022-01-30 | 2022-01-20 4 | 123 | 2022-02-10 | 2022-02-10 4 | 123 | 2022-02-15 | 2022-02-10
db<>fiddle here