简体   繁体   中英

sql ex ru excercise data mismatch question 63

there are two tables

Passenger(ID_psg, name)
Pass_in_trip(trip_no, date, ID_psg, place)

on which you should find the names of passangers who has travelled more than once on the exact same seat more precisely the question is like this:

Find the names of different passengers that ever travelled more than once occupying seats with the same number

both tabels are linked to eachother thropugh the id_psg the column which contains the seat is place from pass_in_trip which contains which passenger name was seated there via id_psg

my current code is:

with a(name, heret) as (
select pr.name , count(p.place) as heret from passenger pr 
join pass_in_trip p on
pr.id_psg = p.id_psg
group by pr.name , p.place
)

select distinct name from a 
where heret > 1

on which i grouped the passengers names with their place(seat), i got the correct result in the first database but it had data mismatch on the second one, can anyone help me to understand what is wrong in this situation?

Name isn't an integer, it is likely a string of some type. You will need to use a COUNT() with a group by quite likely.

looks like the problem was on the join operation this is the correct code for anyone who faces similar obstacle

select name from Passenger where id_Psg IN (
select p.ID_psg from
pass_in_trip p 
group by p.ID_psg, p.place
having count(p.place) > 1 )

search for the names whose id_psg results in that subquerie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM