根据另一个字段的最大值选择记录

问题描述 投票:0回答:1

我正在尝试根据土地面积字段的最大值提取不同城市名称及其纬度和经度的列表。我得到的最好的是“ 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
1个回答
0
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.