In most cases, when you have an aggregate function (MAX in your example), all the columns that are in the select list must either be inside an aggregate function, or they should also be listed in the GROUP BY clause. So your query should be one of these:-- 1SELECT v.sSerialNo, iHardwareID, v.sDescription, (g.iVehicleID), MAX(g.dtDateTime) [DATETIME], g.bIgnition, g.fLatitude, g.fLongitude, g.fSpeedFROM [Donovan].[dbo].[GPSData] AS G JOIN [Donovan].[dbo].[Vehicle] AS V ON v.iID = g.iVehicleIDGROUP BY (v.sSerialNo), iHardwareID, v.sDescription, (g.iVehicleID), g.bIgnition, g.fLatitude, g.fLongitude, g.fSpeed --- 2 SELECT v.sSerialNo, MAX(g.dtDateTime) [DATETIME]FROM [Donovan].[dbo].[GPSData] AS G JOIN [Donovan].[dbo].[Vehicle] AS V ON v.iID = g.iVehicleIDGROUP BY (v.sSerialNo)
But neither of these might be what you are looking for. If you are looking for all the columns corresponding to the latest g.dtDatetime, do the following:SELECT * FROM( SELECT v.sSerialNo, iHardwareID, v.sDescription, (g.iVehicleID), g.dtDateTime [datetime], g.bIgnition, g.fLatitude, g.fLongitude, g.fSpeed, ROW_NUMBER() OVER (PARTITION BY v.sSerialNo ORDER BY g.dtDateTime DESC) AS RN FROM [Donovan].[dbo].[GPSData] AS G JOIN [Donovan].[dbo].[Vehicle] AS V ON v.iID = g.iVehicleID)s WHERE RN = 1;