Code:
SELECT
e.ID
, e.ELR
, e.Track_Id
, e.DB_Start_Mileage
, e.DB_End_Mileage
, (
SELECT
TOP 1 i.[Track Cat]
FROM
qryINM2 AS i
WHERE
(
i.ELR = e.ELR
AND i.[Track Id] = e.Track_Id
AND
(
(
StartMileage <= e.DB_Start_Mileage
AND EndMileage >= e.DB_Start_Mileage
)
OR
(
StartMileage <= e.DB_End_Mileage
AND EndMileage >= e.DB_End_Mileage
)
OR
(
StartMileage >= e.DB_Start_Mileage
AND EndMileage <= e.DB_End_Mileage
)
)
)
GROUP BY
i.[Track Cat]
ORDER BY
Count(i.[Track Cat]) DESC
)
FROM
tblLNE_Eighths AS e