如何确定与count()汇总的非相同值?
我有一个包含三列的表:城市ID,语言代码和城市:
city_id | language_code | city
----------------------------------
1 | en | London
1 | es | Londres
1 | pt | Londres
2 | de | Köln
2 | en | Cologne
3 | it | Cologne
在某些外语中,城市可以以相同的方式拼写,例如伦敦的西班牙语和葡萄牙名称为londres
。
但是在某些情况下,相同名称可以指完全不同的位置,例如cologne
是德国城市科伦市的英文名称,但意大利也有一个同名的小镇。
我希望能够检测表中有多个条目的城市,但只有链接到不同的city_id
值的城市。在我的情况下,这将是cologne
,但不是londres
,因为es
和pt
语言版本指向相同的<代码> city_id 。
我认为这是一件相当容易的事情,但是我无法在单个查询中获得结果。取而代之的是,我首先删除结果,然后汇总它们:
WITH deduped_cities AS (
SELECT DISTINCT city, city_id
FROM cities
ORDER BY city
)
SELECT city, COUNT(city_id) AS total
FROM deduped_cities
GROUP BY city
HAVING COUNT(city_id) > 1;
这给了我预期的结果:
city | total
----------------
Cologne | 2
我只是想知道是否有可能使用单个选择语句实现相同的效果。
I have a table containing three columns: city ID, language code and city:
city_id | language_code | city
----------------------------------
1 | en | London
1 | es | Londres
1 | pt | Londres
2 | de | Köln
2 | en | Cologne
3 | it | Cologne
In some foreign languages cities can be spelled the same way, e.g. the Spanish and Portuguese name for London is Londres
.
But there are cases where the same name can refer to completely different locations, e.g. Cologne
is an English name for the German city of Köln but there's also a town of the same name in Italy.
I would like to be able to detect cities that have more than one entry in the table but only those that are linked to different city_id
values. In my case this would be Cologne
but not Londres
as both es
and pt
language versions point to the same city_id
.
I thought this would be a fairly easy thing to do but I haven't been able to get the results in a single query. Instead, I am deduping the results first and then aggregating them:
WITH deduped_cities AS (
SELECT DISTINCT city, city_id
FROM cities
ORDER BY city
)
SELECT city, COUNT(city_id) AS total
FROM deduped_cities
GROUP BY city
HAVING COUNT(city_id) > 1;
This gives me the expected result:
city | total
----------------
Cologne | 2
I was just wondering if it is possible to achieve the same effect with a single SELECT statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您基本上只有一个
选择
,但是可以使用Dinters
'与count
请参阅 fiddle
you have basically just one
SELECT
, but you can useDISTINCT
' with theCOUNT
See fiddle
我相信您可以在汇总和有子句中进行独特之处:
I believe you can just do the distinct within the aggregate and the having clause: