Input table - t1
make | model | engine | kms_covered | start | end
-------------------------------------------------------
suzuki | sx4 | petrol | 11 | City A | City D
suzuki | sx4 | diesel | 150 | City B | City C
suzuki | swift | petrol | 140 | City C | City B
suzuki | swift | diesel | 18 | City D | City A
toyota | prius | petrol | 16 | City E | City A
toyota | prius | hybrid | 250 | City B | City E
Need to get a subset of the records such that start and end is only cities where both diesel and hybrid cars were either in start or end.
In above case, expect that only city B qualifies for the condition and expect output table as below
output table
make | model | engine | kms_covered | start | end
-------------------------------------------------------
suzuki | sx4 | diesel | 150 | City B | City C
suzuki | swift | petrol | 140 | City C | City B
toyota | prius | hybrid | 250 | City B | City E
Two step process
- Get list of cities where both
dieselandhybridcars have either instartorend - Subset the table with only records having cities in #1
Need help with starting point as below.
select * from t1
where start in () or end in ()