在SQL Server中跨多行添加2个日期之间的时间

时间:2017-01-18 17:11:27

标签: sql-server

我有一张表格列出了我公司的所有用户。每个工作人员都有多个条目,显示他们是如何被雇用的。

RowID   UserID  FirstName   LastName    Title      StartDate    Active  EndDate
-----------------------------------------------------------------------------------
1       1       John        Smith       Manager    2017-01-01   0       2017-01-31 
2       1       John        Smith       Director   2017-02-01   0       2017-02-28 
3       1       John        Smith       CEO        2017-03-01   1       NULL
4       2       Sam         Davey       Manager    2017-01-01   0       2017-02-28 
5       2       Sam         Davey       Manager    2017-03-01   0       NULL
6       3       Hugh        Holland     Admin      2017-02-01   1       NULL
7       4       David       Smith       Admin      2017-01-01   0       2017-02-28

我正在尝试编写一个查询,告诉我在任何给定时间的服务长度。

我遇到问题的部分是因为一个人由多行代表,因为他们的信息会随着时间的推移而变化我需要合并多行......

我有一个问题,要报告在某个时间点就业的人员,这是我已经达到的目的。

DECLARE @DateCheck datetime 
SET @DateCheck = '2017/05/10'

SELECT * 
FROM UsersTest
WHERE @DateCheck >= StartDate AND @DateCheck <= ISNULL(EndDate, @DateCheck)

3 个答案:

答案 0 :(得分:4)

您需要使用datediff函数。关键是选择合适的数字 - 天,月,年。返回值是一个整数,所以如果你选择年份,它将被舍入(并且记住,它将针对每个记录舍入,而不是摘要。我已经选择了几个月。以下内容已被添加以获得最多用户名的最新信息:

WITH CurrentName AS
  (SELECT UserID, FirstName, LastName 
     from
     UserStartStop
     where Active = 1 -- You can replace this with a date check
  ) 

SELECT uss.UserID, 
   MAX(cn.FirstName) as FirstName, -- the max is necessary because we are
                                   -- grouping.  Could include in group by   
   MAX(cn.LastName) as LastName,
    SUM(DATEDIFF(mm,uss.StartDate,COALESCE(uss.EndDate,GETDATE())))
   from UserStartStop uss
   JOIN CurrentName cn
     on uss.UserID = cn.UserID
GROUP BY UserID
order by UserID

答案 1 :(得分:1)

在服务的几个月内,更改&#39; d&#39;到了&#39; mm&#39;:

Create table #UsersTest (
      RowId int
    , UserID int
    , FirstName nvarchar(100)
    , LastName nvarchar(100)
    , Title nvarchar(100)
    , StartDate date
    , Active bit
    , EndDate date)

Insert #UsersTest values (1, 1, 'John', 'Smith', 'Manager', '2017-01-01', 0, '2017-01-31')
Insert #UsersTest values (1, 1, 'John', 'Smith', 'Director', '2017-02-01', 0, '2017-02-28')
Insert #UsersTest values (1, 1, 'John', 'Smith', 'CEO', '2017-03-01', 1, null)
Insert #UsersTest values (1, 2, 'Sam', 'Davey', 'Manager', '2017-01-01', 0, '2017-02-28')
Insert #UsersTest values (1, 2, 'Sam', 'Davey', 'Manager', '2017-03-01', 0, null)
Insert #UsersTest values (1, 3, 'Hugh', 'Holland', 'Admin', '2017-02-01', 1, null)
Insert #UsersTest values (1, 4, 'David', 'Smith', 'Admin', '2017-01-01', 0, '2017-02-28')

Declare @DateCheck as datetime = '2017/05/10'
Select UserID, FirstName, LastName
    , Datediff(d, Min([StartDate]), iif(isnull(Max([EndDate]),'1900-01-01')<@DateCheck, @DateCheck ,Max([Enddate]))) as [LengthOfService]
from #UsersTest
Group by UserID, FirstName, LastName

答案 2 :(得分:-2)

试试吧

   Select 
FirstName, 
LastName,
Min(StartDate)StartDate,
Max(isnull(EndDate,getdate()) as EndDate
    from Table