表列在查询中不可用

时间:2011-12-06 20:29:49

标签: sql-server

我正在使用此查询,但收到错误vdagbk.bedragvdagbk.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

如何使此查询有效?

1 个答案:

答案 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

它应该有用。