我有一个返回下表的查询。 Null表示员工仍在该任务中:
|Dept|EmployeeAssignment|BeginDate |EndDate |
|1003|Analyst |01/01/1990|02/04/2013|
|1002|Coordinator |05/14/2000|06/01/2013|
|1003|Trainer |07/28/2010|NULL |
|1004|Janitor |08/09/2013|NULL |
|1005|IT |09/02/2013|12/21/2013|
我能做些什么可以让我跟踪员工是否在2013年按月出现在该任务中。这样的事情是理想的:
|Dept|EmployeeAssignment|BeginDate |EndDate |Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|
|1003|Analyst |01/01/1990|02/04/2013| 1 | 1 | | | | | | | | | | |
|1002|Coordinator |05/14/2000|06/01/2013| 1 | 1 | 1 | 1 | 1 | 1 | | | | | | |
|1003|Trainer |07/28/2010|NULL | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|1004|Janitor |08/09/2013|NULL | | | | | | | | 1 | 1 | 1 | 1 | 1 |
|1005|IT |09/02/2013|12/21/2013| | | | | | | | | 1 | 1 | 1 | 1 |
提前感谢您提供的任何帮助。
答案 0 :(得分:1)
您可以再次推断其他月份,但我认为这样做很明显。
//using your demo input
DECLARE @tbl TABLE(Dept INT,EmployeeAssignment VARCHAR(20),BeginDate DATE,EndDate DATE)
INSERT INTO @tbl VALUES
(1003,'Analyst' ,'01/01/1990','02/04/2013')
,(1002,'Coordinator' ,'05/14/2000','06/01/2013')
,(1003,'Trainer' ,'07/28/2010',NULL )
,(1004,'Janitor' ,'08/09/2013',NULL )
,(1005,'IT' ,'09/02/2013','12/21/2013')
SELECT *
,CASE WHEN BeginDate <= '2013-01-31' AND ISNULL(EndDate,GETDATE()) >='2013-01-01' THEN 1 ELSE NULL END Jan
,CASE WHEN BeginDate <= '2013-02-28' AND ISNULL(EndDate,GETDATE()) >='2013-02-01' THEN 1 ELSE NULL END Feb
FROM @tbl