I'm trying to get all rows but avoid those with 'departing' entry on "status" column.
I want to know that 'trips' have 'arrived' as 'status' but exclude those that already have 'departing' as 'status'.
I already tried with GROUP BY DISTINCT
(without the desired result).
Datatabse:
id trip type status date
1 1260 ocean arriving 2019-03-04
2 1260 ocean departing 2019-03-05
3 1261 ocean arriving 2019-03-05
4 1262 ocean arriving 2019-03-05
5 1263 ocean arriving 2019-03-08
6 1263 ocean departing 2019-03-09
Query:
SELECT * FROM `test` WHERE `status` NOT IN (select `status` FROM `test` WHERE `status` = 'departing')
Result:
id trip type status date
1 1260 ocean arriving 2019-03-04
3 1261 ocean arriving 2019-03-05
4 1262 ocean arriving 2019-03-05
5 1263 ocean arriving 2019-03-08
Desired result:
id trip type status date
3 1261 ocean arriving 2019-03-05
4 1262 ocean arriving 2019-03-05
Here is the SQL fiddle: http://sqlfiddle.com/#!9/9ec331/3