【问题标题】:SQL Query to to make fromDate and toDate?SQL Query to make fromDate 和 toDate?
【发布时间】:2017-02-10 13:46:56
【问题描述】:

我正在尝试从两个表(例如 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,退出应为 toDate。任何帮助表示赞赏

【问题讨论】:

  • 请将源数据添加为create table ... insert into ... 脚本,以便我们实际使用它来帮助您。

标签: sql database sql-server-2008


【解决方案1】:

这可以通过apply 轻松实现。这些与joins 的不同之处在于它们允许您在查询中引用其他表。在这种情况下,我将过滤apply 中的表以仅保留AppraisalDate 在特定行的Re-Joining 日期之后的Exit 记录,然后选择最近的一个,使用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 |
+------------+-------------------------+-------------------------+

【讨论】:

  • 出色的解决方案。谢谢@iamdave,你拯救了我的一天:)
猜你喜欢
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多