[英]sql ex ru excercise data mismatch question 63
有兩張桌子
Passenger(ID_psg, name)
Pass_in_trip(trip_no, date, ID_psg, place)
您應該在其中找到在完全相同的座位上旅行不止一次的乘客的姓名,更准確地說,問題是這樣的:
Find the names of different passengers that ever travelled more than once occupying seats with the same number
兩個表格都通過 id_psg 彼此鏈接,包含座位的列來自 pass_in_trip,其中包含通過 id_psg 坐在那兒的乘客姓名
我當前的代碼是:
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
我在上面將乘客姓名與他們的位置(座位)分組,我在第一個數據庫中得到了正確的結果,但它在第二個數據庫中的數據不匹配,任何人都可以幫助我理解這種情況下出了什么問題嗎?
名稱不是 integer,它可能是某種類型的字符串。 您很可能需要將 COUNT() 與 group by 一起使用。
看起來問題出在加入操作上,對於面臨類似障礙的任何人來說,這是正確的代碼
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 )
搜索其 id_psg 結果在該子查詢中的名稱
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.