1

我有以下代码,目的是统计每个组中在给定时间范围内离开公司的员工人数。我正在使用 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'));
4

2 回答 2

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)看起来一点也不对 ..(提示:查看 HAVING 子句的条件以获取表单中的有效表达式a1 >= a2 And b1 <= b2。)

此外,请参阅BETWEEN运算符以了解编写此条件的不同/更简洁的方式。

于 2013-01-15T19:59:13.797 回答
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))
于 2013-01-16T14:26:22.293 回答