输入表格 - 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
需要获取记录的子集,以便start
和end
只是diesel
和hybrid
车辆都位于start
或end
。
在上述情况下,期望只有城市B符合条件并期望输出表格如下
输出表格
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
两步流程
diesel
或hybrid
start
和end
辆汽车的城市列表
如下所示,需要有关起点的帮助。
select * from t1
where start in () or end in ()
答案 0 :(得分:1)
with c as (
select city
from (select start as city, engine
from t1
union all
select end, engine
from t1
)
where engine in ('petrol', 'deisel')
group by city
having count(distinct engine) = 2
)
select t1.*
from t1
where t1.start in (select city from c) and
t1.end in (select city from c);