1

I have the following DB Tables with SQL Server

Booking(bookingID,customerName,branchID,RefNumber,...)

Trip(TripID,vehicleID,...)

BookingToTripMap(TripID,bookingID)

Branch(branchID, branchName)

Vehicle(vehicleID,vehicleNumber)

There is a one to one relationship between (Booking,Branch) and (Trip, Vehicle) and Many to many relationship between Booking, Trip which is saved in the table BookingToTripMap.

Now I want to extract a query that would return the following

Booking.RefNumber   Booking.CustomerName    Vehicle.VehicleNumber

(All vehicle numbers in one cell)

8
  • 1
    See at stackoverflow.com/questions/12559551/… Commented May 29, 2013 at 9:40
  • 2
    @nikhilthecoder: How is the Booking table related to Vehicle table. I did not see any relationship or foregien key in any table Commented May 29, 2013 at 9:41
  • Hi deepak, sorry, my mistake, it was bookingID in the Table BookingToTripMap, not vehilceID. Commented May 29, 2013 at 9:44
  • Have you tried something? Post your code Commented May 29, 2013 at 9:52
  • Best if you can also show the entity diagram or relationship information (like many-to-many, one-to-many). Commented May 29, 2013 at 10:30

1 Answer 1

0

Here is your query

SELECT B.RefNumber, B.CustomerName, V.VehicleNumber
FROM ((Booking AS B INNER JOIN BookingToTripMap AS BT
     ON B.bookingID = BT.bookingID) INNER JOIN TRIP as T
     ON T.TripID = BT.TripID) INNER JOIN Vehicle as V
     ON V.vehicleID = T.vehicleID

I would add the field bookingID to the table Trip, it seems that the table BookingToTripMap doesn't add any value to your database.

Also, if your vehicle's numbers are unique, you could change the primary key in the Vehicle table to vehicleNumber, and change the same columns in the Trip table. Thus you could retrieve the vehicleNumber directly from the Trip table.

I'm just guessing in that, based on the given information.

Regards,

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

1 Comment

I assume that the relations between (Branch, Booking) and (Trip, Vehicle) are not one to one, but one to many. Once you fix a Booking, it only corresponds to a Branch, and once you fix a Trip, maybe it has only a correspondent vehicle... But a vehicle makes many trips and the branch many bookings, right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.