我一直在检查有关此错误的堆栈溢出的几个帖子,我在尝试添加SQL查询以检索列tblAR.ARID
时也得到了以下错误:
SELECT tblAR.ARID,
@GP = (Sum(ard.Amount))
-(Sum((CASE WHEN MaterialNumber='REBATE' THEN [qty]
WHEN ard.Amount<0 THEN -1*ABS([qty])
ELSE ABS([qty])
END)))
FROM
tblMMMaterials mm
INNER JOIN tblARDetail ard ON mm.MatID = ard.MatID
INNER JOIN tblAR arh ON ard.ARID = arh.ARID
WHERE arh.Date=@RunDate
我发现我不能在单个查询中同时执行这两项操作。因此,我将查询分为两部分:
SELECT
@GP = (Sum(ard.Amount))
-(Sum((CASE WHEN MaterialNumber='REBATE' THEN [qty]
WHEN ard.Amount<0 THEN -1*ABS([qty])
ELSE ABS([qty])
END)))
FROM
tblMMMaterials mm
INNER JOIN tblARDetail ard ON mm.MatID = ard.MatID
INNER JOIN tblAR arh ON ard.ARID = arh.ARID
WHERE arh.Date=@RunDate
select tblAR.ARID from tblAR
WHERE tblAR.Date=@RunDate
虽然它没有给我任何错误,但我不相信这个后来的sql代码是100%理想的。换句话说,我想知道在第二个查询中包含'WHERE'条件是否足够,即WHERE tblAR.Date=@RunDate
。请帮忙
答案 0 :(得分:0)
由于您有JOIN
参与查询,因此它们不相同。您可以像
SELECT
@GP = (Sum(ard.Amount))
-(Sum((CASE WHEN MaterialNumber='REBATE' THEN [qty]
WHEN ard.Amount<0 THEN -1*ABS([qty])
ELSE ABS([qty])
END)))
FROM
tblMMMaterials mm
INNER JOIN tblARDetail ard ON mm.MatID = ard.MatID
INNER JOIN tblAR arh ON ard.ARID = arh.ARID
WHERE arh.Date=@RunDate;
SELECT tblAR.ARID
FROM
tblMMMaterials mm
INNER JOIN tblARDetail ard ON mm.MatID = ard.MatID
INNER JOIN tblAR arh ON ard.ARID = arh.ARID
WHERE arh.Date=@RunDate