获取Access SQL中所有内容的百分比

时间:2015-08-07 15:40:49

标签: sql ms-access

我有一个表格,其中包含特定团队针对给定版本实施的功能列表,并带有一个标志,告诉我该功能是否可测试。

样本数据可以是:

feature   team         rel       testable
1           1           1            1
2           1           1            1
3           1           1            1
4           1           2            1
5           1           2            1
6           1           2            0
7           1           3            0
8           1           3            0
9           1           3            1
10          2           1            0
11          2           1            0
12          2           1            0
13          2           2            1
14          2           2            0
15          2           2            0
16          2           3            1
17          2           3            1
18          2           3            0

我尝试获得的是,对于每个团队和每个版本,可测试功能的百分比是多少(相对于此团队和发布的 的总体数量

理想情况下,由于我设计显示结果的方式,我希望将其保留为单个SQL查询。

我走了这么远:

SELECT 
    MyTable.team AS team, 
    MyTable.rel AS rel,
    (COUNT(*)*100 / (
        SELECT COUNT(*) 
        FROM MyTable 
        WHERE 
            [MyTable].team = team 
            AND [MyTable].rel = rel
        )
    ) AS result
FROM MyTable 
WHERE 
    MyTable.team IN (1,2) 
    AND MyTable.rel IN (1,2,3) 
    AND MyTable.testable = 1 
GROUP BY 
    MyTable.rel, 
    MyTable.team 
ORDER BY 
    MyTable.team, 
    MyTable.rel

这是我期望的结果(我不太关心四舍五入)

team    rel   result
1        1      1      // all are testable for team 1 release 1
1        2     0.66    // 2 out of 3 are testable for team 1 release 2
1        3     0.33
2        1      0
2        2     0.33
2        3     0.66

我的感觉是我离解决方案的距离不远,但我无法修复它。

2 个答案:

答案 0 :(得分:1)

我认为一个简单的平均函数可以在这里工作;假设可测试字段中的所有值都是1或0。

哦,摆脱where子句

中的testable = 1

我不确定访问是否会隐式转换布尔值...所以这将使avg通过将值明确转换为1,0来实现。

SELECT 
    MyTable.team AS team, 
    MyTable.rel AS rel,
    AVG(iif(Testable,1,0)) AS result
FROM MyTable 
WHERE 
    MyTable.team IN (1,2) 
    AND MyTable.rel IN (1,2,3) 
GROUP BY 
    MyTable.rel, 
    MyTable.team 
ORDER BY 
    MyTable.team, 
    MyTable.rel

答案 1 :(得分:0)

select y.team, y.rel, x.cnt/y.tot as res
from (
select t.team, t.rel, sum(x.cnt) as tot
from (
select team, rel, testable, count(*) as cnt
from table where team in (1,2) and rel in (1,2,3)
group by team, rel, testable) x
join table t on t.team = x.team and t.rel = x.rel
group by team, rel) y

你可以试试这个。