在查询访问中计算计算字段

时间:2018-03-14 10:47:04

标签: sql-server ms-access access-vba

我有一个查询,我想计算未结头寸 - 关闭(计算另一个表中的已关闭行)并排除所有0结果

SELECT Positions.[Req #], tblPositionList.PositionName, 
       Positions.[Opening Date], Positions.[Close Date], 
       Positions.Status, Count([OpenPosition]![Close Date]) AS Closed,
       Positions.Openings AS Opened, [Opened]-[Closed] AS Remaining
FROM tblPositionList INNER JOIN (
       Positions INNER JOIN OpenPosition ON Positions.[Req #] = OpenPosition.[Req #]) 
ON (tblPositionList.PositionID = Positions.PositionID) AND
   (tblPositionList.PositionID = OpenPosition.PositionID)
GROUP BY Positions.[Req #], tblPositionList.PositionName, Positions.[Opening Date], 
         Positions.[Close Date], Positions.Status, Positions.Openings;

我想在Remaining字段中添加一个过滤器。

1 个答案:

答案 0 :(得分:0)

谢谢你,@ user3728595,它完美无缺。 最后的脚本是

 SELECT Positions.[Req #], tblPositionList.PositionName, Positions.[Opening 
    Date], Positions.[Close Date], Positions.Status, Count([OpenPosition]![Close 
    Date]) AS Closed, Positions.Openings AS Opened, Positions.Openings-
    Count([OpenPosition]![Close Date]) AS Remaining
    FROM tblPositionList INNER JOIN (Positions INNER JOIN OpenPosition ON 
    Positions.[Req #] = OpenPosition.[Req #]) ON (tblPositionList.PositionID = 
    Positions.PositionID) AND (tblPositionList.PositionID = 
    OpenPosition.PositionID)
    GROUP BY Positions.[Req #], tblPositionList.PositionName, Positions.[Opening 
    Date], Positions.[Close Date], Positions.Status, Positions.Openings
    HAVING (((Positions.Openings-Count([OpenPosition]![Close Date]))>0));