0

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

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 才能得到百分比。

4

2 回答 2

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;
于 2013-09-10T16:16:41.193 回答
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
于 2013-09-10T16:24:06.143 回答