DIOS
2009-08-20 20:19:00 UTC
I have two tables that are related via a trip id like so:
Trips
-------------------------------------------------------------
TripId TripDate TripNumber Vehicle Driver
1 8/14/06 A200 MX3 Dave Jones
2 8/17/06 A450 DSW Dave Jones
3 8/19/07 C50A TR4 John Doe
Roads
-------------------------------------------------------------
TripId RoadId RoadNumber RoadDist RoadType
1 0 10A 200 HWY
1 1 16E 200 HWY
1 2 12X 210 ACC
2 3 1E 345 HWY
2 4 65 333 SVC
2 5 34R 312 HWY
3 6 10B 99 HWY
3 7 44 99 HWY
3 8 4S 99 SVC
For each trip I want to print out the minimum road distance and
associated roads. the output I need is something like
Trip TripNum MinDist Roads
1 A200 200 10A/16E
2 A450 312 34R
3 C50A 99 10B/44/4S
I can get the minimum distance but cant get the multiple records of
roads that meet that minimum distance in one shot. So far I have
something like
SELECT T.[TripId]
,T.[TripNumber]
,MinDist = (SELECT MIN(R.RoadDist) FROM [Roads] AS R WHERE
R.TripId = T.TripId)
,Roads = ??????
FROM [Trips] AS T
INNER JOIN [Roads] AS R ON (T.TripId=R.TripId)
ORDER BY T.TripNumber
Any help is appreciated. It may be simple but cant seem to find any
examples that will help me complete the SQL.
tia
AGP
Trips
-------------------------------------------------------------
TripId TripDate TripNumber Vehicle Driver
1 8/14/06 A200 MX3 Dave Jones
2 8/17/06 A450 DSW Dave Jones
3 8/19/07 C50A TR4 John Doe
Roads
-------------------------------------------------------------
TripId RoadId RoadNumber RoadDist RoadType
1 0 10A 200 HWY
1 1 16E 200 HWY
1 2 12X 210 ACC
2 3 1E 345 HWY
2 4 65 333 SVC
2 5 34R 312 HWY
3 6 10B 99 HWY
3 7 44 99 HWY
3 8 4S 99 SVC
For each trip I want to print out the minimum road distance and
associated roads. the output I need is something like
Trip TripNum MinDist Roads
1 A200 200 10A/16E
2 A450 312 34R
3 C50A 99 10B/44/4S
I can get the minimum distance but cant get the multiple records of
roads that meet that minimum distance in one shot. So far I have
something like
SELECT T.[TripId]
,T.[TripNumber]
,MinDist = (SELECT MIN(R.RoadDist) FROM [Roads] AS R WHERE
R.TripId = T.TripId)
,Roads = ??????
FROM [Trips] AS T
INNER JOIN [Roads] AS R ON (T.TripId=R.TripId)
ORDER BY T.TripNumber
Any help is appreciated. It may be simple but cant seem to find any
examples that will help me complete the SQL.
tia
AGP