我有以下查询...
SELECT DISTINCT
inv.name,
loc.city,
cls.color
FROM INVENTORY inv
INNER JOIN Model mod ON mod.id = inv.id
LEFT OUTER JOIN Location loc ON loc.id = mod.id
LEFT OUTER JOIN Class cls ON cls.id = mod.id
ORDER BY name, city asc;
这将给我以下内容......
NAME | CITY | Color
1 Bob New York Red
2 Janet Denver Green
3 John New York Blue
但我希望的结果是......
NAME | CITY | Color
1 Bob New York Red
2 John New York Blue
有几个帖子相似但似乎不适用于我的特定查询,因为我使用内部联接和两个左外部联接连接多个表。非常感谢任何帮助!
答案 0 :(得分:0)
如果您只想要出现多次的城市,请使用分析函数:
with t as (
<your query here without the order by>
)
select t.*
from (select t.*, count(*) over (partition by city) as city_cnt
from t
) t
where city_cnt > 1;