我正在为2010年至2013年期间参加各种培训课程的员工汇总一份培训数据表。我按计划获得了这些数据。一些员工的培训可能会在一年内多次意外注册,我只需要个别员工的数量。然而,每年或每隔一年需要进行一些培训,我试图获得每年完成的每项培训的最长日期,因此,如果有人确实参加了一年的课程,一年后又一次,他们被计算在内。我到目前为止的代码是,
SELECT [Training_GEMS].JOB_CLS_CD_DSC_TE, [Training_GEMS].TNG_NA, [Training_GEMS].TNG_SYS_NR, [Training_GEMS].JOB_CLS_CD, sum(iif(Max_Date BETWEEN #1/1/2010# AND #12/31/2010#,1,0)) AS 2010, sum(iif(Max_Date BETWEEN #1/1/2011# AND #12/31/2011#,1,0)) AS 2011, sum(iif(Max_Date BETWEEN #1/1/2012# AND #12/31/2012#,1,0)) AS 2012, sum(iif(Max_Date BETWEEN #1/1/2013# AND #12/31/2013#,1,0)) AS 2013
FROM Training_GEMS
GROUP BY [Training_GEMS].JOB_CLS_CD_DSC_TE, [Training_GEMS].TNG_NA, [Training_GEMS].TNG_SYS_NR, [Training_GEMS].JOB_CLS_CD;
我有SUM IIF设置,我只是想获得每年的最大值。
答案 0 :(得分:0)
这样的事情可能需要两个步骤。第一个是你所描述的消除任何一年中不止一次上课的人。
Select UserID, Year(CourseDate) as CourseYear, Course, Max(CourseDate) as CourseDate
From Training_Gems
Group by Year(CourseDate), UserID, Course;
您可以使用Year()函数从日期中获取年份,而无需使用IIF为每年创建新列。
获得第一个结果后,您可以在第二个子查询中使用它们。
select Count(Course), Course From
(Select UserID, Year(CourseDate) as CourseYear, Course, Max(CourseDate) as CourseDate
From Training_Gems
Group by Year(CourseDate), UserID, Course) as t1
Group By Course;
答案 1 :(得分:0)
添加以下四行(将逗号添加到上一个SUM ... 2013字段后)
Max(Iif(max_date BETWEEN #1 / 1 / 2010 #
AND #12 / 31 / 2010 # , max_date, null)) AS 2010max,
Max(Iif(max_date BETWEEN #1 / 1 / 2011 #
AND #12 / 31 / 2011 # , max_date, null)) AS 2011max,
Max(Iif(max_date BETWEEN #1 / 1 / 2012 #
AND #12 / 31 / 2012 # , max_date, null)) AS 2012max,
Max(Iif(max_date BETWEEN #1 / 1 / 2013 #
AND #12 / 31 / 2013 # , max_date, null)) AS 2013max