I’m trying to create a query where if customers took 1 trip that day, then they would have to pay the price of a single ticket (200). If they took 2 trips on the day, then they would have to pay single * 2. And if they went on 3 or more, then they would have to pay for a day ticket (500). I managed to produce an output, but I don’t understand why the query adds up the single tickets.
This is the database that I’m working on. It’s quite a big one, so I used dbfiddle instead of typing it all out here.
This is the query I tried.
SELECT ct.c_id, DATE(bt.start_time) AS date,
SUM(
CASE
WHEN tc.duration = 'Single' AND trip_count = 1 THEN tc.cost
WHEN tc.duration = 'Single' AND trip_count = 2 THEN tc.cost * 2
ELSE 0
END
) +
SUM(
CASE
WHEN tc.duration = 'Day' AND trip_count >= 3 THEN tc.cost
ELSE 0
END
) AS total_cost
FROM CustomerTrip ct JOIN BusTrip bt ON ct.b_id = bt.b_id
JOIN TicketCosts tc ON tc.duration IN ('Single', 'Day')
JOIN(
SELECT
b_id,
DATE(start_time) AS trip_date,
COUNT(*) AS trip_count
FROM BusTrip GROUP BY b_id, DATE(start_time))
trip_counts ON ct.b_id = trip_counts.b_id AND DATE(bt.start_time) = trip_counts.trip_date
GROUP BY ct.c_id, DATE(bt.start_time);
2
Answers
Make the aggregation of tips an inner subquery, and the once aggregated use subqueries in the result set to calculate cost:
Result:
fiddle
We start with
JOIN
bothCustomerTrip
&BusTrip
, which contain information about customers’ trips and the corresponding bus trips. We use theGROUP BY
clause to group the results by customer IDc_id
and the date of the bus tripDATE(bt.start_time)
. This grouping is needed to sum up number of trips a customer took on a particular day.We can use the
CASE
statement to determine the cost based on thecount(*)
for each group.Here is the demo.
Topics Covered:
subqueries
to fetchticket costs
.CASE
statement is used to apply conditional logic to calculate costs based on the number of trips.COUNT()
function to count the number of trips.JOIN
is used to combine data from two different tables (CustomerTrip
&BusTrip
) based on a common column.WHERE
clauses used to filter data based on specific conditions.GROUP BY
is used to group data bycustomer ID
anddate
.