I want to select data based on a date range in a table and also get the latest value before that date range.
How can I make this more efficient?
The table is defined as:
Name varchar(100) Checked
DataType varchar(100) Checked
Time datetime Checked
Value int Checked
The table contains the following data:
Name DataType Time Value
Name1 Type1 2010-09-29 17:29:00.000 999
Name2 Type2 2013-11-02 06:51:00.000 0
I have created the following query which is a little long winded but it does return the correct results. However, I want to know if this can be achieved using better T-SQL?
I am going to write the code as in-line SQL within my application
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