不完整的输出 ms access 2007

incomplete output ms access 2007

SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed, gen.qty_seed, gen.price

FROM (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod FROM gen GROUP BY hhid, country)  AS temp, gen

WHERE (((temp.hhid)=gen.hhid) And ((temp.country)=gen.country) And ((temp.max_prod)=gen.total_prod) And ((temp.max_area)=gen.area))

ORDER BY temp.hhid;

为什么有些结果没看到?

我至少有100个hhid,每个都有3个区域,3个产品,种子数量,价格等等... 除 1 hhid 外,所有 hhid 都显示在输出查询中,

可能出了什么问题?

问题是prod的最大值和area的最大值很少在同一行。

您还应该学习使用显式 join 语法。一个简单的规则:永远不要在 from 子句中使用逗号。

这可能是您想要的:

SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed,    
       gen.qty_seed, gen.price
FROM gen INNER JOIN
     (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod 
      FROM gen
      GROUP BY hhid, country
     ) AS temp
     ON temp.hhid = gen.hhid AND temp.country = gen.country 
WHERE (temp.max_prod = gen.total_prod or temp.max_area = gen.area)
ORDER BY temp.hhid;

我留下了 WHERE 子句,因为 MS Access 对连接有奇怪的限制。从逻辑上讲,它应该放在 ON 子句中,但我不能 100% 确定 Access 接受该语法。