0

i have a table consist of Truck Id & Mileage where Truck ID can be duplicated (trips). I want a query that shows the Max(mileage)-Min(Mileage) but only if the count of Truck Id > 1. Excuse me i am still a beginner in this world.

2
  • 1
    What did you try? Commented Nov 3, 2016 at 8:54
  • Did our answers help you? Commented Nov 8, 2016 at 9:17

2 Answers 2

2
Select Max(mileage)-Min(Mileage) as diff, TruckID
from tableTruck
group by truckid
having count(truckid) > 1

Use this query

Sign up to request clarification or add additional context in comments.

Comments

2

Try to use GROUP BY ... HAVING:

SELECT MAX(mileage)-MIN(Mileage), Truck_Id
FROM YourTable
GROUP BY Truck_Id
HAVING COUNT(Truck_id) > 1 

A little tutorial with examples on the HAVING clause you find here: http://www.w3schools.com/sql/sql_having.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.