SQL Group By选择%的值

时间:2013-11-25 11:19:55

标签: sql database ms-access count group-by

我正在使用Access(我知道JET SQL与T-SQL不同但是如果你在T-SQL中有答案我将尝试翻译它)并且我有以下内容:

  • 表格部门名称和ID以及其他信息
  • 表具有应用程序列表的应用程序以及它们是否可部署(布尔/位)
  • 表连接DepartmentId和ApplicationId(n:n关系)
  • 的ApplicationMapping

我试图了解每个部门可以部署多少个应用程序(简单),但也知道有多少个不可部署,因此完成百分比。 (或至少类似(count(where deployable = true)/ count(*))

我真的不知道如何进行这样的查询。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

(此答案是在Access 2010下开发和测试的。)

首先,我们需要一个查询,根据部门提供应用程序总数。易:

SELECT DepartmentId, Count(*) AS CountOfApplications
FROM ApplicationMapping
GROUP BY DepartmentId

说给我们

DepartmentId  CountOfApplications
------------  -------------------
           1                    4
           2                    1

您已经有了按部门计算可部署应用程序的查询,这可能看起来像这样

SELECT 
    am.DepartmentId, 
    Sum(IIf(a.Deployable,1,0)) AS CountOfDeployable
FROM
    ApplicationMapping AS am
    INNER JOIN
    Applications AS a
        ON am.ApplicationId = a.ApplicationId
GROUP BY DepartmentId

我们假设它给了我们

DepartmentId  CountOfDeployable
------------  -----------------
           1                  2
           2                  1

然后我们需要做的就是将这两个查询加在一起

SELECT 
    q1.DepartmentId,
    q2.CountOfDeployable / q1.CountOfApplications AS pctDeployable
FROM
    (
        SELECT DepartmentId, Count(*) AS CountOfApplications
        FROM ApplicationMapping
        GROUP BY DepartmentId
    ) AS q1
    INNER JOIN
    (
        SELECT 
            am.DepartmentId, 
            Sum(IIf(a.Deployable,1,0)) AS CountOfDeployable
        FROM
            ApplicationMapping AS am
            INNER JOIN
            Applications AS a
                ON am.ApplicationId = a.ApplicationId
        GROUP BY DepartmentId
    ) AS q2
        ON q1.DepartmentId = q2.DepartmentId

我们得到了

DepartmentId  pctDeployable
------------  -------------
           1            0.5
           2              1

编辑:

如果你想要一个包含所有Departments的列表,即使那些没有任何 Applications的那些,那么只需将整个前面的查询包装在另一个在[Department]表上进行外连接的查询中:

SELECT
    d.DepartmentId,
    d.DepartmentName,
    IIf(IsNull(calc.DepartmentId),0,calc.CountOfApplications) AS totalApplications,
    calc.pctDeployable
FROM
    Department AS d
    LEFT JOIN
    (
        SELECT 
            q1.DepartmentId,
            q1.CountOfApplications,
            q2.CountOfDeployable / q1.CountOfApplications AS pctDeployable
        FROM
            (
                SELECT DepartmentId, Count(*) AS CountOfApplications
                FROM ApplicationMapping
                GROUP BY DepartmentId
            ) AS q1
            INNER JOIN
            (
                SELECT 
                    am.DepartmentId, 
                    Sum(IIf(a.Deployable,1,0)) AS CountOfDeployable
                FROM
                    ApplicationMapping AS am
                    INNER JOIN
                    Applications AS a
                        ON am.ApplicationId = a.ApplicationId
                GROUP BY DepartmentId
            ) AS q2
                ON q1.DepartmentId = q2.DepartmentId
    ) AS calc
        ON d.DepartmentId = calc.DepartmentId

返回

DepartmentId  DepartmentName  totalApplications  pctDeployable
------------  --------------  -----------------  -------------
           1  Department1                     4            0.5
           2  Department2                     1              0
           3  Department3                     0               

答案 1 :(得分:0)

在这种情况下你必须使用联接 -

  • 以下查询应该会给你想要的结果,但在Oracle数据库中。 我不知道如何翻译成Access。

    SELECT department_id,
           total_cnt,
           (deployable_cnt / total_cnt) * 100 deployable_perc,
           100 - (deployable_cnt / total_cnt) * 100 not_deployable_perc
    FROM (SELECT dep.department_id,
                 COUNT(*) total_cnt,
                 SUM(CASE
                      WHEN app.deployable = 'Y' THEN
                      1
                      ELSE
                      0
                     END) deployable_cnt
           FROM department dep, applications app, applicationmapping appmap
          WHERE appmap.application_id = app.application_id
           and  appmap.department_id =  dep.department_id
     -- some other condition, if any
      GROUP BY dep.department_id)