SQL问题:选择没有重复的城市距离
我有一个包含以下列的表:城市,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将距离计算留空,因为这取决于您是否“直线飞行”、使用街道路线、考虑地球曲率等。
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.
您尝试做的事情称为“运行总计”。
请小心三角形连接,例如
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!