[b]电表读数问题,电表每天按固定时间间隔读取数据,报表要求提取出每天8点前的最后一条记录做为前一天的读数,SQL语句怎么写?[/b]
Create Table TEST(ID Int, TestTime DateTime)
Insert TEST Select 1, '2007-03-29 07:30:00'
Union All Select 2, '2007-03-29 08:00:00'
Union All Select 3, '2007-03-30 07:00:00'
Union All Select 4, '2007-03-31 07:00:00'
Union All Select 5, '2007-03-31 07:50:00'
GO
Select * From TEST A
Where Not Exists(Select 1 From TEST Where DateDiff(dd, TestTime, A.TestTime) = 0 And TestTime > A.TestTime And DatePart(Hour, TestTime) < 8)
And DatePart(Hour, TestTime) < 8
GO
Drop Table TEST--Result
ID Test Time
1 2007-03-29 07:30:00.000
3 2007-03-30 07:00:00.000
5 2007-03-31 07:50:00.000
本文介绍了一种SQL查询方法,用于从电表数据中提取每天8点前的最后一条记录作为前一天的读数。通过创建测试表并插入样本数据,演示了如何使用子查询和日期函数来实现这一需求。
1499

被折叠的 条评论
为什么被折叠?



