27,579
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:@ttt
declare @ttt table([id] int,[病人ID] int,[记录日期] datetime,[体温] numeric(3,1))
INSERT @ttt
select 1,1001,'2018-6-1',38 union all
select 2,1001,'2018-6-2',37 union all
select 3,1001,'2018-6-3',37.2 union all
select 4,1001,'2018-6-4',36.8 union all
select 5,1001,'2018-7-6',37.2 union all
select 6,1001,'2018-7-7',37.5 union all
select 7,1001,'2018-7-8',36.8 union all
select 8,1001,'2018-7-9',37.8
SELECT * ,
( SELECT max(id)
FROM @ttt AS B
WHERE B.记录日期 <= A.记录日期
AND not EXISTS ( SELECT 1
FROM @ttt AS C
WHERE B.记录日期 = DATEADD(dd, 1, C.记录日期) )
) AS GRP
FROM @ttt AS A;
/*
id 病人ID 记录日期 体温 GRP
1 1001 2018-06-01 00:00:00.000 38.0 1
2 1001 2018-06-02 00:00:00.000 37.0 1
3 1001 2018-06-03 00:00:00.000 37.2 1
4 1001 2018-06-04 00:00:00.000 36.8 1
5 1001 2018-07-06 00:00:00.000 37.2 5
6 1001 2018-07-07 00:00:00.000 37.5 5
7