SQL问题:选择没有重复的城市距离

发布于 2024-09-11 15:44:31 字数 163 浏览 8 评论 0原文

我有一个包含以下列的表:城市,x,y

我需要选择所有城市之间的距离而不重复 结果应该是这样的 城市1,城市2,距离

但是 我不应该得到 city2,city1,distance (因为我已经得到了这些城市之间的距离...

正确的 SQL 查询是什么... 谢谢!

I have a table with the columns: city, x, y

I need to select the distance between all the cities without duplications
the results should look like this
city1,city2,distance

but
I shouldn't get city2,city1,distance ( since I already got the distance between those cities...

what is the correct SQL query for that...
thanks!

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

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

发布评论

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

评论(3

御弟哥哥 2024-09-18 15:44:32
SELECT c1.City, c2.City, dbo.CalcDistance(c1.x, c1.y, c2.x, c2.y)
FROM Cities c1, Cities c2
WHERE c1.CityID > c2.CityID 
SELECT c1.City, c2.City, dbo.CalcDistance(c1.x, c1.y, c2.x, c2.y)
FROM Cities c1, Cities c2
WHERE c1.CityID > c2.CityID 
人生戏 2024-09-18 15:44:32
SELECT
    C1.city,
    C2.city,
    <distance calculation>
FROM
    Cities C1
INNER JOIN Cities C2 ON
    C2.city_id > C1.city_id

我将距离计算留空,因为这取决于您是否“直线飞行”、使用街道路线、考虑地球曲率等。

SELECT
    C1.city,
    C2.city,
    <distance calculation>
FROM
    Cities C1
INNER JOIN Cities C2 ON
    C2.city_id > C1.city_id

I left the distance calculation blank since it depends on whether you're doing "as the crow flies", using street routes, accounting for curvature of the earth, etc.

最美的太阳 2024-09-18 15:44:32

您尝试做的事情称为“运行总计”。

请小心三角形连接,例如 ON c1.CityID > c2.CityID,他们创建笛卡尔积! (我不想与其他人的答案争论。)

看看 Jeff Moden 关于运行总计的世界最佳文章:
解决“运行总计”和“运行总计”问题“序数排序”问题(重写)

如果您没有帐户,则必须创建一个帐户(只需要一个电子邮件地址)。不过,我保证这是值得的!

What you are trying to do is called a "running total".

Be careful with triangular joins like ON c1.CityID > c2.CityID, they create an cartesian product! (I don't want to argue with anybody else's answer.)

Have a look at Jeff Moden's world best article about running totals:
Solving the "Running Total" & "Ordinal Rank" Problems (Rewritten).

If you don't have an account you will have to create one (only an email address is needed). However, I promise it's worth!

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