2011-12-06 79 views
-1

我正在使用此查詢,但出現錯誤vdagbk.bedragvdagbk.reden表列在查詢中不可查表

問題是,如果我在第二個SELECT中輸入,結果肯定會是錯誤的,因爲表中的double數據會將結果總結多次。

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

你給你的表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 

,它應該工作。