I'm on MYSQL 5.6 with a single table called ride
+---------+-------+---------+
| Date | CarId | Airport |
+---------+-------+---------+
| 2001-11 | 1 | JFK |
| 2001-11 | 2 | JFK |
| 2001-11 | 3 | LAX |
| 2001-12 | 1 | JFK |
| 2001-12 | 2 | JFK |
| 2001-12 | 3 | JFK |
+---------+-------+---------+
In the column date, there are a lot of dates with duplicates. For a list of all distinct dates I run this query:
SELECT DISTINCT date from ride;
And I got two dates ✅
My goal is to know the number of cars at every recorded time on a specific airport. So I think I have to join the same table.
SELECT DISTINCT r.date, count(x.CarId) as car from ride r JOIN ride x on r.date = x.date WHERE x.airport = "LAX";
Here I got only one row with date 2001-11 and 3 cars. ❌
What is the correct query to get something like this:
2001-11 LAX 1 car
Or is it impossible with the data structure? 2001-12 LAX 0 cars