如何从特定日期持续时间中查找活动/非活动状态

时间:2014-02-22 11:52:04

标签: sql-server-2008

示例:(检查活动和非活动)

持续时间 - 自日期:2011-01-01            至日期:2014-01-01

EMPID   ACTIVEDATE/REJOINING DATE       INACTIVEDATE

-----   ------------------------        -----------

1       NULL                           '2011-12-31'

2       '2012-01-01'                   '2011-12-31'

2       NULL                           '2013-12-31'

3       '2015-01-01'                   '2011-12-31'

我需要以下输出:

Active 

EMPID 
-----  

在Active

EMPID   
-----

1

2

3

请发送确切的SQL查询并满足我的条件。

此致 维诺

1 个答案:

答案 0 :(得分:0)

试试这个......

SELECT EmployeeID,CASE WHEN (ISNULL(MAX([REJOINING DATE]),'') > GETDATE()) OR (MAX(INACTIVEDATE) >ISNULL(MAX([REJOINING DATE]),'') ) THEN 'INACTIVE'
                       WHEN  (MAX(INACTIVEDATE) < ISNULL(MAX([REJOINING DATE]),'') ) AND (ISNULL(MAX([REJOINING DATE]),'') < GETDATE())  THEN 'ACTIVE'
                       ELSE  'N/A'
                 END AS 'Status'
 FROM @Table