我有一个50办公室的列表和一个很大的县列表,我试图确定哪些是离这些县最近的top 3办公室。我已经使用县中心和办公室的Lat Long来计算到每个中心的距离。
我的数据现在降到了3列:
Zip, OfficeName, and Miles (the distance between the two)
Zip OfficeName Miles
20601 Park Potomac 32.1344
20601 Clarksville 39.6714
20601 Cambridge 43.1868
20601 Ellicott City 44.4464
20601 Lutherville 55.8513
20601 Perry Hall 56.0435
20602 Park Potomac 33.3036
20602 Clarksville 41.9749
20602 Cambridge 45.3606
20602 Ellicott City 47.0838
20602 Lutherville 58.8546
20602 Perry Hall 59.2286
20603 Park Potomac 30.0754
20603 Clarksville 39.6311
20603 Ellicott City 45.1373
20603 Cambridge 48.3889我有所有50个办公室的里程数,但如何减少最近3个办公室的输出。
我已经尝试了在以下位置找到的解决方案:Access 2002 - how to Group By and Top N records in same query
SELECT TopDistance.Zip, TopDistance.OfficeName, TopDistance.Miles
FROM TopDistance
WHERE TopDistance.Miles In
(SELECT TOP 3 TopDistance.Miles
FROM TopDistance as Dupe
WHERE Dupe.Zip = TopDistance.Zip and Dupe.OfficeName=TopDistance.OfficeName
ORDER BY TopDistance.Miles ASC)基于SELECT TOP 3语句的使用,我应该只获得每个Zip的3行数据,显示关闭的三个办公室以及这些办公室有多远。
但是,结果仍然显示了所有50个办公室的距离。
为什么使用"Select Top 3“不起作用?
发布于 2019-06-07 02:35:43
我认为您应该在子查询中将select输出从TopDistance.Miles更改为Dupe.Miles。其次,子查询中的条件是to detailed。这个条件Dupe.Zip = TopDistance.Zip and Dupe.OfficeName=TopDistance.OfficeName只能由一行来满足,这就是为什么你得到了所有的行。
SELECT TopDistance.Zip, TopDistance.OfficeName, TopDistance.Miles
FROM TopDistance as TopDistance
WHERE TopDistance.Miles In
(SELECT TOP 3 Dupe.Miles
FROM TopDistance as Dupe
WHERE Dupe.Zip = TopDistance.Zip
ORDER BY Dupe.Miles ASC)https://stackoverflow.com/questions/56483144
复制相似问题