计算认证经理的百分比

时间:2011-06-23 15:30:35

标签: sql ms-access ms-access-2007

几周前我问了一个类似的问题,但现在要求已经改变了。

考虑以下表格: http://www.maroisconsulting.com/Temp/query.png

我需要创建一个查询,该查询返回经理(Titles.IsManager)员工的百分比以及在认证字段(Employees.Certified)中有日期的人员。结果需要按每个商店所在的集团进行分组。

到目前为止,我有这个:

SELECT d.GroupId, 
       Sum(d.cert_complete) AS SumOfcert_complete, 
       Count(d.cert_complete) AS CountOfcert_complete
FROM (SELECT DISTINCT
        s.GroupId,
        e.EmployeeID,
        IIf(e.Certified Is Null,0,1) AS cert_complete
    FROM
        ((Stores AS s
        INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
        INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
        INNER JOIN Titles AS t ON e.TitleId = t.TitleId
    )  AS d
WHERE t.IsManager
GROUP BY d.GroupId;

然后这个

SELECT q.GroupId, 
      (SumOfcert_complete/CountOfcert_complete)*100 AS percent_certified, 
      Groups.GroupName
FROM qryGroupCert_base AS q 
INNER JOIN Groups ON q.GroupId = Groups.GroupId;

您可以在第一个查询中看到我添加了标题表。

1)我被提示输入IsManager,虽然我不知道为什么 2)返回的结果与我添加IsManager

之前的结果没有什么不同

有人在这里看到了什么问题吗?

非常感谢

2 个答案:

答案 0 :(得分:0)

也许您需要为t.IsManager提供标准,例如t.IsManager = TRUE。如果where子句没有设置值等于的值,则Access可能不会将其解析为实际列,但认为它是查询参数。

答案 1 :(得分:0)

在第一个查询中,您有此子查询,其中包含别名为“t”的标题:

(SELECT DISTINCT
    s.GroupId,
    e.EmployeeID,
    IIf(e.Certified Is Null,0,1) AS cert_complete
FROM
    ((Stores AS s
    INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
    INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
    INNER JOIN Titles AS t ON e.TitleId = t.TitleId
)  AS d

然后,子查询的定义之后,你有了这个WHERE子句:

WHERE t.IsManager

问题是“t”别名和IsManager列只存在于子查询中 - >它们对于外(父)查询是未知的。如果Access数据库引擎遇到无法识别为对象名称,函数,文字值或SQL关键字的内容,则认为某些内容必须是参数...因此弹出输入框,要求您提供(IsManager)参数的值。

我认为你应该在子查询定义中移动WHERE子句。

SELECT d.GroupId, 
       Sum(d.cert_complete) AS SumOfcert_complete, 
       Count(d.cert_complete) AS CountOfcert_complete
FROM [SELECT DISTINCT
        s.GroupId,
        e.EmployeeID,
        IIf(e.Certified Is Null,0,1) AS cert_complete
    FROM
        ((Stores AS s
        INNER JOIN EmployeeStores AS es ON s.StoreId = es.StoreId)
        INNER JOIN Employees AS e ON es.EmployeeId = e.EmployeeID)
        INNER JOIN Titles AS t ON e.TitleId = t.TitleId
        WHERE t.IsManager = True
    ]. AS d
GROUP BY d.GroupId;