1.创建测试表
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[testA]') AND type IN ('U'))
DROP TABLE [dbo].[testA]
GO
CREATE TABLE [dbo].[testA] (
[id] int NOT NULL,
[code] varchar(255) COLLATE Chinese_PRC_CI_AS NULL,
[name] varchar(255) COLLATE Chinese_PRC_CI_AS NULL,
[tm] datetime NULL,
[valueN] int NULL
)
GO
ALTER TABLE [dbo].[testA] SET (LOCK_ESCALATION = TABLE)
GO
-- ----------------------------
-- Records of [testA]
-- ----------------------------
INSERT INTO [dbo].[testA] VALUES (N'1', N'001', N'张三', N'2021-03-10 16:18:33.000', N'10')
GO
INSERT INTO [dbo].[testA] VALUES (N'2', N'001', N'张三', N'2021-03-09 16:18:33.000', N'9')
GO
INSERT INTO [dbo].[testA] VALUES (N'3', N'001', N'张三', N'2021-03-08 16:18:33.000', N'8')
GO
INSERT INTO [dbo].[testA] VALUES (N'4', N'001', N'张三', N'2021-03-07 16:18:33.000', N'7')
GO
INSERT INTO [dbo].[testA] VALUES (N'5', N'001', N'张三', N'2021-03-06 16:18:33.000', N'6')
GO
INSERT INTO [dbo].[testA] VALUES (N'6', N'001', N'张三', N'2021-03-05 16:18:33.000', N'5')
GO
INSERT INTO [dbo].[testA] VALUES (N'7', N'001', N'张三', N'2021-03-04 16:18:33.000', N'4')
GO
INSERT INTO [dbo].[testA] VALUES (N'8', N'001', N'张三', N'2021-03-03 16:18:33.000', N'3')
GO
INSERT INTO [dbo].[testA] VALUES (N'9', N'001', N'张三', N'2021-03-02 16:18:33.000', N'2')
GO
INSERT INTO [dbo].[testA] VALUES (N'10', N'001', N'张三', N'2021-03-01 16:18:33.000', N'7')
GO
INSERT INTO [dbo].[testA] VALUES (N'11', N'001', N'张三', N'2021-03-01 08:00:00.000', N'0')
GO
INSERT INTO [dbo].[testA] VALUES (N'12', N'002', N'李四', N'2021-03-10 08:00:33.000', N'9')
GO
INSERT INTO [dbo].[testA] VALUES (N'13', N'002', N'李四', N'2021-03-09 08:00:33.000', N'99')
GO
INSERT INTO [dbo].[testA] VALUES (N'14', N'002', N'李四', N'2021-03-08 08:00:33.000', N'88')
GO
INSERT INTO [dbo].[testA] VALUES (N'15', N'002', N'李四', N'2021-03-05 08:00:33.000', N'9')
GO
INSERT INTO [dbo].[testA] VALUES (N'16', N'002', N'李四', N'2021-03-01 08:18:33.000', N'9')
GO
INSERT INTO [dbo].[testA] VALUES (N'17', N'003', N'王五', N'2021-03-10 16:18:33.000', N'100')
GO
INSERT INTO [dbo].[testA] VALUES (N'18', N'003', N'王五', N'2021-03-05 16:18:33.000', N'50')
GO
INSERT INTO [dbo].[testA] VALUES (N'19', N'003', N'王五', N'2021-03-01 16:18:33.000', N'60')
GO
INSERT INTO [dbo].[testA] VALUES (N'20', N'003', N'王五', N'2021-03-01 08:00:00.000', NULL)
GO
-- ----------------------------
-- Primary Key structure for table testA
-- ----------------------------
ALTER TABLE [dbo].[testA] ADD CONSTRAINT [PK__testA__3213E83FAB649435] PRIMARY KEY CLUSTERED ([id])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
GO
2.执行SQL,查看执行结果
-- 本条SQL 用于查询每条数据的前后数据值
SELECT
preData=(SELECT top 1 tm FROM testA WHERE tm < t1.tm ORDER BY tm desc), -- 上条数据的时间
nextData=(SELECT top 1 tm FROM testA WHERE tm > t1.tm ORDER BY tm asc), -- 下条数据的时间
t1.tm,-- 本条数据时间
t1.code,
t1.name,
t1.valueN,-- 本条数据值
nextvalueN=(SELECT top 1 valueN FROM testA WHERE tm > t1.tm ORDER BY tm asc) -- 下条数据的值
FROM testA t1
WHERE t1.tm>'2021-03-01 08:00' and '2021-03-10 08:00' > t1.tm
GROUP BY t1.code,t1.name,t1.tm,t1.valueN
3.完成