我正在使用此查询,但收到错误vdagbk.bedrag
和vdagbk.reden
。
问题是,如果我在第二个SELECT中输入结果肯定是错误的,因为由于表中的双重数据,它会多次将结果相加。
SELECT TOP (100) PERCENT
SUM(P.bedrag) AS ex,
C.dosno,
C.dosnm,
SUM(P.betaald) AS TotBetaald,
SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS Expr1
FROM
dbo.verkopen AS P
INNER JOIN
(SELECT DISTINCT
dbo.doss.dosno, dbo.doss.dosnm, dbo.verkopen.ino
FROM dbo.verkopen
INNER JOIN dbo.doss ON dbo.verkopen.ino = dbo.doss.ino
INNER JOIN dbo.vdagbk ON dbo.verkopen.ino = dbo.vdagbk.ino
WHERE
(dbo.doss.uitvoerder LIKE 'LL')
AND (dbo.doss.dosno LIKE '101520')
GROUP BY
dbo.doss.dosno, dbo.doss.dosnm, dbo.verkopen.ino) AS C ON C.ino = P.ino
INNER JOIN
dbo.vdagbk AS vdagbk_1 ON P.ino = vdagbk_1.ino
GROUP BY
C.dosno, C.dosnm
如何使此查询有效?
答案 0 :(得分:0)
您在此处dbo.vdagbk
使用别名vdagbk_1
:
INNER JOIN
dbo.vdagbk AS vdagbk_1 ON P.ino = vdagbk_1.ino
因此您还需要在SUM()
子句中使用别名:
SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS Expr1
将其更改为:
SUM(CASE vdagbk_1.reden WHEN 'H' THEN vdagbk_1.bedrag END) AS Expr1
它应该有用。