sql min max

Hi

What is the most performant way to select the min and max value

here is my query

select (min odometer) and (max odometer) 
From MyTable
Where gpsDateTime between '2018-01-01' and '2018-01-17' and eventID = 1

Open in new window


i basically need  to return 2 item, the max odometer and min odometer reading based on the datetime
I'm looking for the most efficient SQL that can achieve this

SQL server 2014
websssCEOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
That's pretty much it.

select min(odometer) as odometer_min, max(odometer) as odometer_max
From dbo.MyTable
Where gpsDateTime >= '2018-01-01' and gpsDateTime < '2018-01-18' and eventID = 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
First of all: Use correct SQL. This includes using an save date literal format.

SELECT MIN(odometer) AS min_odometer ,
       MAX(odometer) AS max_odometer
FROM   MyTable
WHERE  gpsDateTime
       BETWEEN '20180101' AND '20180117'
       AND eventID = 1;

Open in new window


Performance depends on correct indices. Thus you need a combined index over (eventID, gpsDateTime, odometer) to cover your query.
0
websssCEOAuthor Commented:
sorry ste5an awarded too soon
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.