【问题标题】:incomplete output ms access 2007 [closed]不完整的输出 ms 访问 2007 [关闭]
【发布时间】:2026-02-03 15:50:01
【问题描述】:
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 ,每个 hhid 有 3 个区域,3 个产品,种子数量,价格等... 除了 1 个 hhid 之外,所有的 hhid 都显示在输出查询中,

可能出了什么问题?

【问题讨论】:

  • 这和 .htaccess 有什么关系?
  • 咨询我的水晶球,我看到...其中一行不符合选择标准。不幸的是,所说的水晶球需要清洁,所以它可能是错误的。

标签: sql ms-access-2007 max average


【解决方案1】:

问题是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 接受该语法。

【讨论】:

  • 你的代码已经工作了,但它仍然给了我相同的输出,丢失的 1 或 2 个 hhid 仍然丢失,你的代码和我的代码提供相同的输出
  • @bentetres 。 . .您确定在 where 子句中有 or 吗?此代码应至少输出每个非 NULL hhidcountry 组合一次。
  • 哦 .. 抱歉,我将 or 更改为和 .. 你能向我解释一下,为什么使用并给我一些缺失的输出? while 或给我完整的答案?
  • @bentetres 。 . .就像我在答案中所说的那样,最大为prod 的行不一定最大为area。对于and,一行必须同时满足两个条件。