获得访问百分比

时间:2013-09-10 15:00:32

标签: sql ms-access

我有一份报告,列出了在调查中回答特定问题的员工人数,并列出了总人口。我试图得到总人口的答案百分比,但我无法让代码工作。到目前为止,我的代码是:

SELECT 
Mgmt_Lvl.MGMT_LVL, 
Count(O896IA_VEMPPRSA.SYS_EMP_ID_NR) AS Total, 
Sum(IIf(emp_rlc_ir="Y",1,0)) AS Relocate, 
Sum(IIf(emp_int_asn_ir="Y",1,0)) AS International_Assignment, 
Sum(IIf(emp_tvl_ir="y",1,0)) AS Travel, 
Sum(IIf(emp_spc_asn_ir="y",1,0)) AS Special_Assignment
FROM (O867IA_VPJOBCO 
INNER JOIN Mgmt_Lvl ON O867IA_VPJOBCO.JOB_GRP_CD = Mgmt_Lvl.JOB_GRP_CD) 
INNER JOIN (O896IA_VEMPPRSA INNER JOIN O867IA_VCAREER 
    ON O896IA_VEMPPRSA.SYS_EMP_ID_NR = O867IA_VCAREER.SYS_EMP_ID_NR) 
ON O867IA_VPJOBCO.JOB_CLS_CD = O896IA_VEMPPRSA.JOB_CLS_CD
WHERE (((O896IA_VEMPPRSA.EMP_EMT_STS_CD)<>"T"))
GROUP BY Mgmt_Lvl.MGMT_LVL;

SUM(IIF(...是计数,TOTAL是总人口。我需要得到SUM(IIF(/ TOTAL来获得百分比。

2 个答案:

答案 0 :(得分:2)

假设您的查询正在运行...将其包装在子查询中。

SELECT MGMT_LVL,
       R / TTL AS RelocatePercent,
       I / TTL AS IntlAssgntPercent,
       T / TTL AS TravelPercent,
       S / TTL AS SpecialAssgntPercent
FROM (SELECT Mgmt_Lvl.MGMT_LVL, 
             Count(O896IA_VEMPPRSA.SYS_EMP_ID_NR) AS TTL, 
             Sum(IIf(emp_rlc_ir="Y",1,0)) AS R, 
             Sum(IIf(emp_int_asn_ir="Y",1,0)) AS I, 
             Sum(IIf(emp_tvl_ir="y",1,0)) AS T, 
             Sum(IIf(emp_spc_asn_ir="y",1,0)) AS S
        FROM (O867IA_VPJOBCO 
                INNER JOIN Mgmt_Lvl
                   ON O867IA_VPJOBCO.JOB_GRP_CD = Mgmt_Lvl.JOB_GRP_CD) 
                INNER JOIN (O896IA_VEMPPRSA INNER JOIN O867IA_VCAREER 
                                               ON O896IA_VEMPPRSA.SYS_EMP_ID_NR = 
                                                  O867IA_VCAREER.SYS_EMP_ID_NR) 
                   ON O867IA_VPJOBCO.JOB_CLS_CD = O896IA_VEMPPRSA.JOB_CLS_CD
       WHERE (((O896IA_VEMPPRSA.EMP_EMT_STS_CD)<>"T"))
    GROUP BY Mgmt_Lvl.MGMT_LVL) SQ;

答案 1 :(得分:1)

您可以在Access中的SELECT查询的其他计算字段中使用别名字段名称。以下修改后的SELECT子句应该执行您想要的操作:

SELECT 
Mgmt_Lvl.MGMT_LVL, 
Count(O896IA_VEMPPRSA.SYS_EMP_ID_NR) AS Total, 
Sum(IIf(emp_rlc_ir="Y",1,0))/Total AS Relocate, 
Sum(IIf(emp_int_asn_ir="Y",1,0))/Total AS International_Assignment, 
Sum(IIf(emp_tvl_ir="y",1,0))/Total AS Travel, 
Sum(IIf(emp_spc_asn_ir="y",1,0))/Total AS Special_Assignment