首页 > 解决方案 > 根据另一个字段的最大值选择记录

问题描述

我正在尝试根据土地面积字段的最大值提取不同城市名称及其纬度和经度的列表。我得到的最好的结果是“SQL:关联字段错误”错误。

SELECT a.primary_city as city ;
    , a.state as state_id ;
    , SPACE(30) as state_name ;
    , a.approximate_latitude as latitude ;
    , a.approximate_longitude as longitude ;
FROM citystate a ;
WHERE a.area_land = ;
    (SELECT MAX(VAL(b.area_land)) ;
    FROM citystate b ;
    WHERE (b.primary_city = a.primary_city ;
        AND b.state = a.state)) ;
GROUP BY a.primary_city ;
    , a.state ;
    , a.approximate_latitude ;
    , a.approximate_longitude 

不确定这甚至会起作用,所以希望得到一些帮助。

谢谢。

标签: visual-foxpro

解决方案


VFP 不支持此类 SQL。你可以这样写:

SELECT csa.primary_city as city ;
    , csa.state as state_id ;
    , SPACE(30) as state_name ;
    , csa.approximate_latitude as latitude ;
    , csa.approximate_longitude as longitude ;
FROM citystate csa ;
INNER JOIN ; 
    (SELECT primary_city, state, MAX(VAL(area_land)) as maxALand ;
    FROM citystate ;
    GROUP BY primary_city, state ) csb ;
  ON csb.primary_city = csa.primary_city ;
        AND csb.state = csa.state ;
WHERE VAL(csa.area_land) = csb.maxALand

推荐阅读