我不知道为什么这会变得无效,我无法弄明白。当我的主管离开时,我得到了一个遗留数据库,我负责直到有人来取代他。我正在尝试运行此查询...
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH INNER JOIN
tblM ON tblCH.guidMId = tblM.guidMId INNER JOIN
ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
我一直得到的错误是...... Msg 8120,Level 16,State 1,Line 1 列'tblM.guidRId'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
为什么会出现此错误?
答案 0 :(得分:1)
因为如果要使用SUM()[或COUNT,AVG()等等]聚合函数和另一个非聚合列,需要GROUP BY:
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId;
答案 1 :(得分:1)
你忘了组guidRId。 (您正在汇总数据)
SELECT
tblM.guidRId,
SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE
tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId
答案 2 :(得分:0)
尝试:
SELECT
tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
GROUP BY tblM.guidRId