oracle - sql query- group by and then join -
i have 2 tables follow:
1)passenger - passenger_id,passenger_name , passenger_city 2)flight - flight_id,flight_name , passenger_id.
the question is:
list passenger details flight id, has travelled in more 1 flight. (this function display passenger details flight id's has travelled in more 1 flight.)
i used query:
select * passenger_1038299 passengerid in(select passengerid flight_1038299 group passengerid having count(passengerid)>1);
but doesnt give me flight_ids. please tell how retrieve flight id well. , sorry stupid question new sql.
join flight table passenger's flights
select * passenger_1038299 p join flight_1038299 f on f.passenger_id = p.passenger_id p.passengerid in( select passengerid flight_1038299 group passengerid having count(passengerid)>1 );
i use exists
check multiples. index on passenger_id
may run faster query above.
select * passenger_1038299 p join flight_1038299 f on f.passenger_id = p.passenger_id exists ( select 1 flight_1038299 f2 f2.passenger_id = f.passenger_id , f2.flight_id <> f.flight_id )
edit
another way using count
window function:
select * ( select *, count() on (partition p.passenger_id) cnt passenger_1038299 p join flight_1038299 f on f.passenger_id = p.passenger_id ) t cnt > 1
Comments
Post a Comment