我想根据表格中的日期范围选择数据,并获取该日期范围之前的最新值。
我怎样才能使它更有效率?
该表定义为:
Name varchar(100) Checked
DataType varchar(100) Checked
Time datetime Checked
Value int Checked
该表包含以下数据:
Name DataType Time Value
Name1 Type1 2010-09-29 17:29:00.000 999
Name2 Type2 2013-11-02 06:51:00.000 0
我创建了以下查询,虽然有点冗长,但它确实返回了正确的结果。但是,我想知道这是否可以使用更好的 T-SQL 来实现?
我将在我的应用程序中将代码编写为内联 SQL
DECLARE @effectiveFrom DATETIME
SET @effectiveFrom = '2010-09-29 17:30:00'
DECLARE @effectiveTo DATETIME
SET @effectiveTo = '2010-09-29 17:49:00'
DECLARE @bmunit varchar(100)
DECLARE @datatype varchar(100)
--Select * from [Table]
--where (Time >= '2010-09-29 17:30:00' and Time < '2010-09-29 17:49:00')
--union Select top 1 * from [Table]
-- where Time < '2010-09-29 17:30:00' order by Time desc
SELECT P1.[Name]
,P1.[DataType]
,P1.[Time]
,P1.[Value]
,P1.Que
FROM
(
SELECT [Name]
,[DataType]
,[Time]
,[Value]
, 'between' AS [Que]
FROM [Table]
WHERE Time >= @effectiveFrom AND EffectiveTime < @effectiveTo
) P1
UNION SELECT P2.[Name]
,P2.[DataType]
,P2.[Time]
,P2.[Value]
, 'before' AS [Que]
FROM
(
SELECT TOP 1 [Name]
,[DataType]
,Time]
,[Value]
FROM [Table] P1
WHERE Time < @effectiveFrom
) P2 --ON P1.[Name] = P2.[Name] AND P1.[DataType] = P2.[DataType]
WHERE Name = 'MyName' --AND P1.Time >= @effectiveFrom AND P1.Time < @effectiveTo
ORDER BY 2,3