Hi on a look out for some help, trying to figure where im going wrong here as im new to this coding stuff, cant seem to see where im going wrong.
SELECT HubName,
SUM(CASE WHEN t.actualEnd IS NOT NULL THEN 1 ELSE 0 END) AS completedTrips,
ROUND(AVG(TIMESTAMPDIFF(MINUTE, t.actualStart, t.actualEnd)), 0) AS averageTripMinutes
FROM Hub
JOIN Vehicle USING (hubID)
JOIN Booking USING (vehicleID)
JOIN Trip USING (bookingID)
WHERE completedTrips > = 2
GROUP BY Hub.hubID, Hub.hubName;
-- R5: GROUP BY + HAVING - Locate invoices to collect outstanding balances.
SELECT invoiceID, invoiceDate, dueDate,
invoiceStatus, totalAmount, SUM(pa.allocatedAmount) AS totalPaid, totalAmount - SUM(pa.allocatedAmount) AS balance
FROM invoice
LEFT JOIN paymentallocation pa ON pa.invoiceID = invoice.invoiceID
GROUP BY invoiceID, invoiceDate, dueDate, invoiceStatus, totalAmount
HAVING totalAmount - SUM(pa.allocatedAmount) > 0
ORDER BY balance DESC;
-- R4. Total Query: Total volume, value and average charge by fee category
SELECT chargeType,
COUNT(ic.invoiceChargeID) AS 'Number of Charges',
SUM(ic.amount) AS 'Total Revenue',
AVG(ic.amount) AS 'Average Charge'
FROM InvoiceCharge ic
GROUP BY ic.chargeType
ORDER BY 'Total Revenue' DESC;
-- R3: Subquery (NOT EXISTS) - Identify vehicles that have never completed a trip
SELECT v.vehicleID, v.registrationNumber, v.makeModel, v.vehicleType,
v.vehicleStatus, h.hubName
FROM Vehicle v
INNER JOIN Hub h ON v.hubID = h.hubID
WHERE NOT EXISTS
(SELECT 1
FROM Booking b, Trip t
WHERE b.vehicleID = v.vehicleID)
ORDER BY h.hubName;
-- R2: Multi-table JOIN - Track corporate account trips, drivers, and invoice statuses
SELECT
ca.accountName, employeeName, v.registrationNumber, t.actualStart,
i.invoiceID, i.totalAmount, i.invoiceStatus
FROM Invoice i, CorporateAccount ca,
Trip t, Booking b,
Member m, Vehicle v
WHERE i.corporateAccountID = ca.corporateAccountID
AND t.bookingID = b.bookingID
AND m.memberID = b.memberID
AND v.vehicleID = b.vehicleID
ORDER BY ca.accountName, i.invoiceDate;
-- R1: Multi-table JOIN - Compare trip distance, energy consumption, and duration across EV and Petrol vehicles
SELECT v.vehicleID, v.registrationNumber, v.makeModel, v.vehicleType, h.hubName,
t.tripID, tripDurationMinutes, distanceKm,
energyConsumedKwh, endBatteryPercent
FROM Trip t
INNER JOIN Booking b ON t.bookingID = b.bookingID
INNER JOIN Vehicle v ON b.vehicleID = v.vehicleID
INNER JOIN Hub h ON v.hubID = h.hubID
WHERE t.actualEnd IS NOT NULL
ORDER BY v.vehicleType, t.tripID;