子查询不起作用

时间:2014-01-10 16:55:10

标签: sql ms-access

由于某种原因,我的运行计数列在任何地方返回460,这是我表中第一个邮政编码的总体人口值的总和(在4个不同的邮政编码中)。

有人可以告诉我我的代码有什么问题吗?感谢:

SELECT
target_postcodes.target_postcode as carehome,
population_postcodes.population_postcode as postcodes,
population_postcodes.population as over85,
SQR( (Population_postcodes.Longitude - target_postcodes.longitude)^2 + (Population_postcodes.Latitude - target_postcodes.latitude)^2 ) as distance,
(SELECT sum (population_postcodes.population) as runningcount from population_postcodes) AS runningcount

INTO
cooltable2

FROM
Population_postcodes, Target_postcodes

GROUP BY
target_postcodes.target_postcode,
population_postcodes.population_postcode,
population_postcodes.population,
SQR( (Population_postcodes.Longitude - target_postcodes.longitude)^2 + (Population_postcodes.Latitude - target_postcodes.latitude)^2 )

ORDER BY
SQR( (Population_postcodes.Longitude - target_postcodes.longitude)^2 + (Population_postcodes.Latitude - target_postcodes.latitude)^2 );

由于

2 个答案:

答案 0 :(得分:0)

您需要添加一个连接条件,指示一个表的哪些行属于另一个表的哪些行。

答案 1 :(得分:0)

两项:

1)sum()函数应用于population_postcodes表上的子查询,该表按条件使所有组无效。

2)没有连接两个表的子句Population_postcodes,Target_postcodes

查询可能应该是...... .... / / p>

   SELECT
      b.target_postcode as carehome,
      a.population_postcode as postcodes,
      a.population as over85,
      SQR( (a.Longitude - b.longitude)^2 + (a.Latitude - b.latitude)^2 ) as distance,
      sum (a.population) as runningcount
   INTO
      cooltable2
   FROM
      Population_postcodes a, Target_postcodes b
   GROUP BY
      b.target_postcode,
      a.population_postcode,
      a.population,
      SQR( (a.Longitude - b.longitude)^2 + (a.Latitude - b.latitude)^2 )
   ORDER BY
      SQR( (a.Longitude - b.longitude)^2 + (a.Latitude - b.latitude)^2 )