表字段无法绑定

时间:2011-12-06 14:55:49

标签: sql-server

这适用于MS SQL:

SELECT     TOP (100) PERCENT SUM(P.bedrag) AS ex
FROM         dbo.verkopen AS P INNER JOIN
                      (SELECT DISTINCT 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')) AS C ON C.ino = P.ino

我必须这样做,因为我的结果中有一些行重复。

但我想用它做这样的事情:

SELECT     TOP (100) PERCENT doss.dosno, doss.dosnm, SUM(P.bedrag) AS TotBedraggefactureerd, SUM(P.betaald) AS TotBetaald, SUM(P.totincl) AS TotIncl, 
                  SUM(vdagbk.beginsaldo) AS totbeginsaldoontvangen, SUM(vdagbk.eindsaldo) AS toteindsaldoontvangen, 
                  SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS ontvangenhonoraria, SUM(CASE vdagbk.reden WHEN 'P' THEN vdagbk.bedrag END) 
                  AS ontvangenprov, SUM(CASE vdagbk.reden WHEN 'A' THEN vdagbk.bedrag END) AS dagboekA, SUM(CASE vdagbk.reden WHEN 'G' THEN vdagbk.bedrag END) 
                  AS dagboekG, SUM(CASE vdagbk.reden WHEN 'E' THEN vdagbk.bedrag END) AS dagboekE
FROM         dbo.verkopen AS P INNER JOIN
                      (SELECT DISTINCT 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')) AS C ON C.ino = P.ino
 GROUP BY doss.dosno, doss.dosnm
 ORDER BY doss.dosno

我知道这一切都搞砸了。我可以做一点SQL,但这有点重新排列。

我知道它只包含.ino但是我现在在查询中放置了什么。

我有这个:

`SELECT        TOP (100) PERCENT doss.dosno, doss.dosnm, SUM(verkopen.bedrag)/2 AS TotBedraggefactureerd, SUM(verkopen.betaald) AS TotBetaald, SUM(verkopen.totincl) 
                     AS TotIncl, SUM(vdagbk.beginsaldo) AS totbeginsaldoontvangen, SUM(vdagbk.eindsaldo) AS toteindsaldoontvangen, 
                     SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS ontvangenhonoraria, SUM(CASE vdagbk.reden WHEN 'P' THEN vdagbk.bedrag END) 
                     AS ontvangenprov, SUM(CASE vdagbk.reden WHEN 'A' THEN vdagbk.bedrag END) AS dagboekA, SUM(CASE vdagbk.reden WHEN 'G' THEN vdagbk.bedrag END) 
                     AS dagboekG, SUM(CASE vdagbk.reden    WHEN 'E' THEN vdagbk.bedrag END) AS dagboekE
FROM            verkopen INNER JOIN
                     doss ON verkopen.ino = doss.ino INNER JOIN
                     vdagbk ON verkopen.ino = vdagbk.ino
 WHERE        (dbo.doss.uitvoerder LIKE 'LL') AND (dbo.doss.dosno LIKE '101520')    GROUP BY doss.dosno, doss.dosnm
ORDER BY doss.dosno
`

但在某些情况下,结果会返回两倍。

2 个答案:

答案 0 :(得分:0)

在您的SELECT中,只有别名PC可用。

dossvdagbk别名是派生表的内部:并且派生表只返回dbo.verkopen.ino

我会考虑删除派生表并直接加入

答案 1 :(得分:0)

您的ino是主键,是吗?在这种情况下,您应该通过查找一些具体示例来调查重复项。在3个源表中查找ino个值。这些表中至少有一个可能具有重复值。