0
表有這樣的結構:grouptable將> lineGroupJoinTable> linesTable(名字被混淆的名字) 我有以下查詢返回此錯誤:意外「子查詢返回多個值」
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
的事情是,我期望,因爲子查詢是組,我不應該得到這個錯誤。我可能錯過了一些東西。
UPDATE dbo.groupTable
SET fieldToUpdate = CASE WHEN fieldToUpdate IS NULL THEN NULL ELSE
(
SELECT sumTable.fieldToSum FROM
dbo.groupTable gt
INNER JOIN
(
SELECT lgjt.groupdId1, lgjt.groupdId2, SUM(lt.fieldToSum) as fieldToSum
FROM lineGroupJoinTable lgjt
INNER JOIN linesTable lt
ON
lt.fieldToSum IS NOT NULL AND lt.fieldToSum > 0 AND
lgjt.lineId1 = lt.lineId1 AND lgjt.lineId2 = lt.lineId2
GROUP BY lgjt.groupdId1, lgjt.groupdId2
) sumTable
ON sumTable.groupdId1 = gt.groupdId1 AND sumTable.groupdId2 = gt.groupdId2
)
END
這種變化還嘗試以下建議,但返回相同的錯誤:
UPDATE dbo.groupTable
SET fieldToUpdate = CASE WHEN fieldToUpdate IS NULL THEN NULL ELSE
(
SELECT SUM(sumTable.fieldToSum) FROM
dbo.groupTable gt
INNER JOIN
(
SELECT lgjt.groupdId1, lgjt.groupdId2, SUM(lt.fieldToSum) as fieldToSum
FROM lineGroupJoinTable lgjt
INNER JOIN linesTable lt
ON
lt.fieldToSum IS NOT NULL AND lt.fieldToSum > 0 AND
lgjt.lineId1 = lt.lineId1 AND lgjt.lineId2 = lt.lineId2
GROUP BY lgjt.groupdId1, lgjt.groupdId2
) sumTable
ON sumTable.groupdId1 = gt.groupdId1 AND sumTable.groupdId2 = gt.groupdId2
GROUP BY gt.groupdId1, gt.groupdId2
)
END
我試過了,結果很相似。鑑於id1和id2是主鍵,我不指望id1,id2對有多行。 – TTT