Home > Software engineering >  MYSQL Having trouble doing a GROUP BY and getting data from 4 database tables
MYSQL Having trouble doing a GROUP BY and getting data from 4 database tables

Time:02-17

I have 4 database tables Rental Car Company Database, Rental Car Details Database, Rental Car Rates Database, and Booked Car Database.

There are multiple Rental Car companies and locations that have multiple cars with each car having different rates ranges,

What I'm trying to do is query all these databases to find out what locations have only trucks available for dates 03/04/22 - 03/10/22.

I want each location to show how many trucks are available at each location along with the lowest daily rate and the highest daily rate.

The problem is each truck has different prices, so when I do a GROUP BY Location its giving the high and low rate of only one truck when two are available. My low should be the lowest dailyrate of the two and the high rate should be the highest daily rate of the two for the dates i have searched.

The result I get with the data and code below is:

Location CarCount Type RentalCarCompanyName lowrate Highrate
LosAngeles 1 Truck Car Lux 281 281
NewYork 2 Truck Discount Car Rental 125 125

What I'm trying to get is:

Location CarCount Type RentalCarCompanyName lowrate Highrate
LosAngeles 1 Truck Car Lux 281 281
NewYork 2 Truck Discount Car Rental 125 169

I have four database tables like the following

Rental Car Company Database Table Name: (geolocations)

Location Address CompanyName
NewYork Queens, NY 11430 Discount Car Rental
LosAngeles 1 World Way, Los Angeles, CA 90045 Car Lux

Rental Car Details Database Table Name: (Cars)

Location Car Type
NewYork NYCar1 Truck
NewYork NYCar2 Truck
NewYork NYCar3 Car
NewYork NYCar4 Truck
LosAngeles LACar1 Truck
LosAngeles LACar2 Truck
LosAngeles LACar3 Truck
LosAngeles LACar4 Van

Rental Car Rates Database Table Name: (prices)

Car fromdate todate dayrate
NYCar1 2022-01-01 2022-04-01 199
NYCar1 2022-04-01 2022-07-01 499
NYCar1 2022-07-01 2022-12-31 300
NYCar2 2022-01-01 2022-04-01 125
NYCar2 2022-04-01 2022-07-01 399
NYCar2 2022-07-01 2022-12-31 200
NYCar3 2022-01-01 2022-04-01 169
NYCar3 2022-04-01 2022-07-01 267
NYCar3 2022-07-01 2022-12-31 250
NYCar4 2022-01-01 2022-04-01 169
NYCar4 2022-04-01 2022-07-01 267
NYCar4 2022-07-01 2022-12-31 250
LACar1 2022-01-01 2022-04-01 281
LACar1 2022-04-01 2022-07-01 267
LACar1 2022-07-01 2022-12-31 267
LACar2 2022-01-01 2022-04-01 300
LACar2 2022-04-01 2022-07-01 250
LACar2 2022-07-01 2022-12-31 267
LACar3 2022-01-01 2022-04-01 500
LACar3 2022-04-01 2022-07-01 700
LACar3 2022-07-01 2022-12-31 400
LACar4 2022-01-01 2022-04-01 500
LACar4 2022-04-01 2022-07-01 700
LACar4 2022-07-01 2022-12-31 400

Booked Car Database Table Name: (bookings)

Car ArrivalDate DepartureDate Name
NYCar1 2022-03-01 2022-03-19 Mike Smith
LACar2 2022-03-08 2022-03-16 Joe Luck
LACar3 2022-03-05 2022-03-25 Kelly Johnson

The code I have tried is below

SELECT Cars.Location, COUNT(Cars.Location) AS CarCount, Cars.Type,
        (SELECT CompanyName
        FROM geolocations
        WHERE geolocations.Location = Cars.Location) AS RentalCarCompanyName,
                        
        (SELECT MIN(dayrate)
        FROM prices
        WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS lowrate,
                       
        (SELECT MAX(dayrate)
        FROM prices
        WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS highrate                  
FROM Cars
WHERE Car NOT IN (
    SELECT Car
    FROM      bookings
    WHERE      (
        ArrivalDate <= '2022-03-14' AND
        DepartureDate > '2022-03-10'
    ) 
) AND Cars.Type = 'TRUCK'                
GROUP BY Location  
ORDER BY Location ASC

I have a fiddle with data loaded

CodePudding user response:

Here is your updated query

SELECT 
  `cars`.`Location`, 
  COUNT(`cars`.`Location`) AS CarCount, 
  `cars`.`Type`, 
  `geolocations`.`CompanyName`, 
  MIN(`prices`.`dayrate`) AS lowrate, 
  MAX(`prices`.`dayrate`) AS highrate 
FROM 
  `cars` 
  JOIN `geolocations` ON `cars`.`Location` = `geolocations`.`Location` 
  JOIN `prices` ON `cars`.`Car` = `prices`.`Car` 
WHERE 
  `cars`.Car NOT IN (
    SELECT 
      Car 
    FROM 
      bookings 
    WHERE 
      (
        ArrivalDate <= '2022-03-14' 
        AND DepartureDate > '2022-03-10'
      )
  ) 
  AND Cars.Type = 'TRUCK' 
  AND `prices`.`fromdate` <= '2022-03-14' 
  AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' 
GROUP BY 
  Location 
ORDER BY 
  Location ASC
  • Related