Avatar of JoeBo747
JoeBo747

asked on 

Find record with nearest Date.

I have a table with transactions which include a date column with a British format date. I want to query  this table to return the transaction with the nearest date to my known date where the vehicle is equal to X9ABC and the status is between 1 and 5 and the date is 07/10/2011 14:15:16. The table has 150000 plus records and performance should be considered. I have seen examples using datediff  and Top(1) but don’t know how to build the query for my requirements. Anyone help with this.

Table Structure
Vehicle            Date             Status             Driver
X9ABC            07/10/2011 09:31:22            2            Bob
X9ABC            07/10/2011 11:01:32        5            Bob
X8ABC                              07/10/2011 18:11:09            6            John


Microsoft SQL Server

Avatar of undefined
Last Comment
JoeBo747
ASKER CERTIFIED SOLUTION
Avatar of Eyal
Eyal
Flag of Israel image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

What type is the date column? If it's a datetime field it is totally unimportant what format you see it in, the datetime binary format is far from the human readable format anyway, and that is what is stored and what you work on, not the human readable format.

Bye, Olaf.
Avatar of Amick
Amick
Flag of United States of America image

I don't have a working SQL Server to test the syntax on, but it would be something like:
select top 1 *, abs( DATEDIFF(second, '07/10/2011 14:15:16',tablename.Date))  as DD 
from table 
where vehicle="X9ABC" and
status  between 1 and 5
order by DD

Open in new window

Avatar of JoeBo747
JoeBo747

ASKER

Hi Eyal

I have tried your suggestion but it does not return a record.

SELECT     dwJobAuditId, Vehicle
FROM         JobAudit
WHERE     (dwJobAuditId IN
                          (SELECT TOP (1) dwJobAuditId
                            FROM  JobAudit AS JobAudit_1
                            ORDER BY ABS(DATEDIFF(s, dtDate, CONVERT(datetime, '07/10/2011 14:15:16', 103))))) AND (Vehicle = '[X9ABC]')
Avatar of Eyal
Eyal
Flag of Israel image

insure that the inner query returns record
Avatar of JoeBo747
JoeBo747

ASKER

Hi Eyal

That’s where the issue was, if I write the query as:
SELECT     TOP (1) dwJobAuditId, Registration, dtDate
FROM         JobAudit AS JobAudit_1
WHERE     (Vehicle= ' X9ABC')
ORDER BY ABS(DATEDIFF(s, dtDate, CONVERT(datetime, '07/10/2011 14:15:16', 103)))

The query then returns the record I expect without resorting to the subquery.  Would you use this method ?
Avatar of Eyal
Eyal
Flag of Israel image

it's even better then my version :) use that
Avatar of JoeBo747
JoeBo747

ASKER

Thanks for both of your comments. But you provided the solution so the points are yours.

Thanks
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo