如何确定与count()汇总的非相同值?

发布于 2025-01-23 07:17:46 字数 1106 浏览 0 评论 0原文

我有一个包含三列的表:城市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,因为espt语言版本指向相同的<代码> 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.

DB Fiddle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2025-01-30 07:17:46

您基本上只有一个选择,但是可以使用Dinters'与count

SELECT city, COUNT(city_id) AS total
FROM cities
GROUP BY city
HAVING COUNT( DISTINCT city_id) > 1;

请参阅 fiddle

you have basically just one SELECT, but you can use DISTINCT' with the COUNT

SELECT city, COUNT(city_id) AS total
FROM cities
GROUP BY city
HAVING COUNT( DISTINCT city_id) > 1;

See fiddle

颜漓半夏 2025-01-30 07:17:46

我相信您可以在汇总和有子句中进行独特之处:

select city, count (distinct city_id)
from cities
group by city
having count (distinct city_id) > 1

I believe you can just do the distinct within the aggregate and the having clause:

select city, count (distinct city_id)
from cities
group by city
having count (distinct city_id) > 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文