SUM IIF BETWEEN

时间:2013-01-15 19:49:32

标签: ms-access count

我有以下代码,目的是计算在给定时间范围内离开公司的每个组中的员工数量。我正在使用COUNT IIF,并且在代码的每一位中AND之后的< =符号是错误的。我该如何计算日期范围呢?

    SELECT O867IA_VPJOBCO.JOB_GRP_CD, Count(IIf(O867IA_VJOBHST.REC_EFF_STT_DT>=#1/1/2009# And <= #12/31/2009#,2009,0)) AS 2009, Count(IIf(O867IA_VJOBHST.REC_EFF_STT_DT>=#1/1/2010# And <= #12/31/2010#,2010,0)) AS 2010, COUNT(IIF(O867IA_VJOBHST.REC_EFF_STT_DT >= #1/1/2011# AND <= #12/31/2011#, 2011, 0)) AS 2010, COUNT(IIF(O867IA_VJOBHST.REC_EFF_STT_DT >= #1/1/2012# AND <= #12/31/2012#, 2012, 0)) AS 2012,
FROM (O867IA_VJOBHST INNER JOIN O867IA_VACTRSN ON (O867IA_VJOBHST.EMP_ACN_RSN_TYP_CD = O867IA_VACTRSN.EMP_ACN_RSN_TYP_CD) AND (O867IA_VJOBHST.EMP_ACN_TYP_CD = O867IA_VACTRSN.EMP_ACN_TYP_CD)) INNER JOIN O867IA_VPJOBCO ON O867IA_VJOBHST.JOB_CLS_CD = O867IA_VPJOBCO.JOB_CLS_CD
WHERE (((O867IA_VJOBHST.REC_EFF_STT_DT)>=#1/1/2009# And (O867IA_VJOBHST.REC_EFF_STT_DT)<=#12/31/2012#) AND ((O867IA_VACTRSN.EMP_ACN_TYP_CD)<>"RMT"))
GROUP BY O867IA_VPJOBCO.JOB_GRP_CD
HAVING (((O867IA_VPJOBCO.JOB_GRP_CD)>='72' And (O867IA_VPJOBCO.JOB_GRP_CD)<='94'));

2 个答案:

答案 0 :(得分:1)

从有问题的表达开始:

O867IA_VJOBHST.REC_EFF_STT_DT>=#1/1/2009# And <= #12/31/2009#

然后简化它(这里我已经替换了上面表达式的a1,a2和b2):

a1 >= a2 And <= b2

放入显示意图的括号(这是解析器的功能,但它确实有助于识别问题):

(a1 >= a2) And (<= b2)

嗯。不,(<= b2)看起来并不正确..(提示:以a1 >= a2 And b1 <= b2形式的有效表达式条件查看HAVING子句。)

另外,请参阅BETWEEN运算符,了解编写此条件的其他/更清晰的方法。

答案 1 :(得分:1)

我已将您的第一个计算列作为示例:

Count(IIf(O867IA_VJOBHST.REC_EFF_STT_DT>=#1/1/2009# And <= #12/31/2009#,2009,0))

在这些情况下,我认为您需要Sum方法而不是Count,请看一下:

Sum(IIf([O867IA_VJOBHST].[REC_EFF_STT_DT] BETWEEN #1/1/2009# And #12/31/2009#,1,0))