找不到SQL列

时间:2015-10-22 11:44:28

标签: sql foxpro

我正在尝试构建一个SQL查询来交叉检查几列。 我的问题是calcontvalue列。 查询工作正常,直到我想要包含一个包含它的Where子句AND calcontvalue = job.contvalue

SELECT 
job.accountno, 
job.groupno, 
job.contvalue, 
job.z1total, 
job.z2equip, 
job.z3freight, 
job.z4pack, 
job.z5ancil, 
job.z6roy, 
job.zrpay, 
job.z7paid, 
job.z8invtype,
IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil, 
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue, 
group.groupno, 
group.grouptype, 
group.title  FROM job

LEFT JOIN group  
ON job.groupno = group.groupno 
WHERE grouptype = 'J' AND calcontvalue = job.contvalue

我遇到了这个错误:

  

SQL:找不到列“CALCONTVALUE”。

不确定接下来要尝试什么。

1 个答案:

答案 0 :(得分:4)

您无法在WHERE中使用别名,请使用完整表达式:

SELECT 
  job.accountno, 
  job.groupno, 
  job.contvalue, 
  job.z1total, 
  job.z2equip, 
  job.z3freight, 
  job.z4pack, 
  job.z5ancil, 
  job.z6roy, 
  job.zrpay, 
  job.z7paid, 
  job.z8invtype,
  IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil, 
  IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue, 
  group.groupno, 
  group.grouptype, 
  group.title  
FROM job
LEFT JOIN group  
  ON job.groupno = group.groupno 
WHERE grouptype = 'J' 
  AND IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil, 
      IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) = job.contvalue

同样命名表group是不好的做法,因为GROUP是关键字GROUP BY