从隐藏列计算总和

时间:2013-11-05 14:00:03

标签: sql ms-access

我有一张表格如下:

enter image description here

罢工率基本上是OrdersTaken - OrdersFrom,但我不想要显示这两列,所以我只想要Code + Strike Rate Calls

但是当我禁用show时,它会提示我要求输入数字,所以它不能再找到它了?

enter image description here

我的查询类似于:

SELECT T_Temp_RestrictedDiaryCalls.AccreditedDomainCode,
    Count(T_Temp_RestrictedProductSalesHistory.CustomerCode)
        AS OrdersTaken, qry_TargetReports.CountOfCustomerCode 
        AS OrdersFrom, [OrdersTaken]-[OrdersFrom] 
        AS [Strike Rate (Calls)]

    FROM qry_TargetReports 
        INNER JOIN (T_Temp_RestrictedDiaryCalls
        INNER JOIN T_Temp_RestrictedProductSalesHistory 
            ON (T_Temp_RestrictedDiaryCalls.CallDate = T_Temp_RestrictedProductSalesHistory.EntryDate)
                AND (T_Temp_RestrictedDiaryCalls.CustomerCode = T_Temp_RestrictedProductSalesHistory.CustomerCode)) 
            ON qry_TargetReports.AccreditedDomainCode = T_Temp_RestrictedDiaryCalls.AccreditedDomainCode

    GROUP BY T_Temp_RestrictedDiaryCalls.AccreditedDomainCode, qry_TargetReports.CountOfCustomerCode;

但说实话,MsAccess会为我生成它。我只需要一种方法来了解如何隐藏两列,同时仍然计算总和,欢呼。

2 个答案:

答案 0 :(得分:1)

使用除[OrdersTaken]-[OrdersFrom]之外的所有内容作为子查询,并在父查询中计算差异。

问题是[OrdersTaken][OrdersFrom]都是字段表达式的别名,原始查询在计算SELECT时尝试在[Strike Rate (Calls)]子句中使用这些别名。通常Access不会让你这样做。

SELECT
    sub.AccreditedDomainCode,
    (sub.OrdersTaken - sub.OrdersFrom) AS [Strike Rate (Calls)]
FROM
    (
        SELECT
            rdc.AccreditedDomainCode,
            Count(rpsh.CustomerCode) AS OrdersTaken,
            tr.CountOfCustomerCode AS OrdersFrom
        FROM
            qry_TargetReports AS tr
            INNER JOIN (T_Temp_RestrictedDiaryCalls AS rdc
            INNER JOIN T_Temp_RestrictedProductSalesHistory AS rpsh
            ON
                    (rdc.CallDate = rpsh.EntryDate)
                AND (rdc.CustomerCode = rpsh.CustomerCode)) 
            ON tr.AccreditedDomainCode = rdc.AccreditedDomainCode
        GROUP BY rdc.AccreditedDomainCode, tr.CountOfCustomerCode
    ) AS sub;

答案 1 :(得分:0)

查看SELECT:有4个部分对应4个结果列

T_Temp_RestrictedDiaryCalls.AccreditedDomainCode,
Count(T_Temp_RestrictedProductSalesHistory.CustomerCode) AS OrdersTaken,
qry_TargetReports.CountOfCustomerCode AS OrdersFrom
[OrdersTaken]-[OrdersFrom] AS [Strike Rate (Calls)]

如果您只想要第1和第4行,只需删除第2行和第3行:

T_Temp_RestrictedDiaryCalls.AccreditedDomainCode,
[OrdersTaken]-[OrdersFrom] AS [Strike Rate (Calls)]

但是,你还必须用它们的定义替换OrdersTaken和OrdersFrom:

T_Temp_RestrictedDiaryCalls.AccreditedDomainCode,
Count(T_Temp_RestrictedProductSalesHistory.CustomerCode) # this is what OrdersTaken stands for
    - qry_TargetReports.CountOfCustomerCode # this is what OrdersFrom stands for
AS [Strike Rate (Calls)]

请注意,您也可以设置

Strike Rate (Calls):Count(T_Temp_RestrictedProductSalesHistory.CustomerCode)-qry_TargetReports.CountOfCustomerCode

在编辑器的查询定义中,这也是一样的。