访问-如何添加2列并减去1

时间:2018-10-07 08:39:41

标签: ms-access

我已经上传了以下屏幕截图和SQL查询作为参考。我要实现的目的是通过将字段“ SubscriptionFee”和“ UNIFORM_UniformPrice”加在一起,然后减去“ UNIFORM_SUPPLY_UniformPrice”的总和来找出盈亏。

相当多的[SubscriptionFee] + [UNIFORM_UniformPrice]-[UNIFORM_SUPPLY_UniformPrice] 是我要尝试的方法,但我不知道如何使它起作用。

当我尝试运行时:

Sum([UNIFORM_UniformPrice]+[SubscriptionFee]-[UNIFORM_SUPPLY_UniformPrice]) AS Total

弹出错误提示:

  

您的查询未将指定的表达式“ Subscriptionfee”作为   聚合函数

表面上的修复方法似乎是在添加“分组依据”,但是我不知道如何键入它,或者这是否是解决我问题的方法。

SQL查询:

SELECT subscription.subscriptionfee, 
   uniform.uniformprice        AS UNIFORM_UniformPrice, 
   uniform_supply.uniformprice AS UNIFORM_SUPPLY_UniformPrice, 
   SUM([uniform_uniformprice] + [subscriptionfee] 
       - [uniform_supply_uniformprice]) 
                               AS Expr1 
FROM   (((uniform_supply 
      INNER JOIN staff_order 
              ON uniform_supply.[uniformsupplyid] = 
                 staff_order.[uniformsupplyid]) 
     INNER JOIN uniform 
             ON uniform_supply.[uniformsupplyid] = 
    uniform.[uniformsupplyid]) 
    INNER JOIN (subscription 
                INNER JOIN student_order 
                        ON subscription.[subscriptionno] = 
                           student_order.[subscriptionid]) 
            ON uniform.[uniformproductid] = 
   student_order.[uniformproductid]) 
   INNER JOIN invoice 
           ON student_order.[orderid] = invoice.[orderid]; 

enter image description here

1 个答案:

答案 0 :(得分:0)

在这种情况下,您不应使用SUM。例如,您不会通过使用group子句进行汇总。

您不能在定义别名的同一选择中将别名作为UNIFORM_UniformPrice引用。

您的声明应类似于:

SELECT subscription.subscriptionfee, 
   uniform.uniformprice        AS UNIFORM_UniformPrice, 
   uniform_supply.uniformprice AS UNIFORM_SUPPLY_UniformPrice, 
   uniform.uniformprice + subscriptionfee 
       - uniform_supply.uniformprice 
                               AS Expr1 
FROM   (((uniform_supply 
      INNER JOIN staff_order 
              ON uniform_supply.[uniformsupplyid] = 
                 staff_order.[uniformsupplyid]) 
     INNER JOIN uniform 
             ON uniform_supply.[uniformsupplyid] = 
    uniform.[uniformsupplyid]) 
    INNER JOIN (subscription 
                INNER JOIN student_order 
                        ON subscription.[subscriptionno] = 
                           student_order.[subscriptionid]) 
            ON uniform.[uniformproductid] = 
   student_order.[uniformproductid]) 
   INNER JOIN invoice 
           ON student_order.[orderid] = invoice.[orderid];