SQL Query来制作fromDate和toDate?

时间:2017-02-10 13:46:56

标签: sql database sql-server-2008

我正在尝试从两个表创建查询,比如Employee和Appraisal。这两个表都有一对多的关系。根据AppraisalType列,评估表记录员工加入,重新加入或退出时的员工记录。

select dbo.Employee.EmployeeID
      ,dbo.Employee.FullName
      ,dbo.Employee.CostCenterID
      ,ea.AppraisalDate as fromDate
      ,
       (select case
                   when exists
                               (select 1
                                from
                                     EmployeeAppraisal as EA2
                                where EA2.EmployeeID = Employee.EmployeeID
                                      and EA2.AppraisalType = 'Exit'
                                having min(EA2.AppraisalDate) > EA.AppraisalDate
                               )
                       then
                            (select min(AppraisalDate)
                             from
                                  EmployeeAppraisal as EA2
                             where EA2.EmployeeID = EA.EmployeeID
                                   and EA2.AppraisalType = 'Exit'
                             having min(EA2.AppraisalDate) > EA.AppraisalDate
                            )
                   else getdate()
               end
       ) as ToDate
      ,0 as MonthDiff
      ,dbo.Employee.IsActive
from
     dbo.Employee
    join EmployeeAppraisal as EA
        on Employee.EmployeeID = EA.EmployeeID
where EA.AppraisalType = 'Re-Joining'
      and Employee.EmployeeId = 1253;

这给出了以下结果集。它为所有行提供了相同的错误toDate

虽然实际数据是,但请注意重新加入日期应为fromDate,Exit应为toDate。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

使用apply可以轻松实现这一目标。它们与join的不同之处在于它们允许您引用查询中的其他表。在这种情况下,我正在过滤apply中的表格,只保留Exit在特定行的AppraisalDate日期之后的Re-Joining记录,然后选择最新的记录一个,top 1order by

declare @e table(EmployeeID int);
insert into @e values(1253);

declare @a table(EmployeeID int,AppraisalDate datetime,AppraisalType nvarchar(10));
insert into @a values
 (1253,'20140101','Exit')
,(1253,'20140601','Re-Joining')
,(1253,'20150101','Exit')
,(1253,'20150601','Re-Joining')
,(1253,'20160101','Exit')
,(1253,'20160601','Re-Joining')
,(1253,'20170101','Exit')
,(1253,'20170601','Re-Joining')
,(1253,'20180101','Exit');


select e.EmployeeID
    ,f.AppraisalDate as FromDate
    ,t.AppraisalDate as ToDate
from @e e
    join @a f
        on e.EmployeeID = f.EmployeeID
            and f.AppraisalType = 'Re-Joining'
    outer apply (select top 1 AppraisalDate
                    from @a tt
                    where tt.EmployeeID = e.EmployeeID
                        and tt.AppraisalDate > f.AppraisalDate
                        and tt.AppraisalType = 'Exit'
                    order by tt.AppraisalDate
                ) t;

输出:

+------------+-------------------------+-------------------------+
| EmployeeID |        FromDate         |         ToDate          |
+------------+-------------------------+-------------------------+
|       1253 | 2014-06-01 00:00:00.000 | 2015-01-01 00:00:00.000 |
|       1253 | 2015-06-01 00:00:00.000 | 2016-01-01 00:00:00.000 |
|       1253 | 2016-06-01 00:00:00.000 | 2017-01-01 00:00:00.000 |
|       1253 | 2017-06-01 00:00:00.000 | 2018-01-01 00:00:00.000 |
+------------+-------------------------+-------------------------+