Вот то, чем я закончил. Это работает с интерфейсной программой Cisco, которую мы используем.
DECLARE @dtStartDateTime DATETIME, @dtEndDateTime DATETIME, @agentid VARCHAR
SET @dtStartDateTime = :start_date SET @dtEndDateTime = :end_date
SELECT
FullName = Temp1.FullName,
AgentID = Temp1.SkillTargetID,
LogonDate = Temp1.LogonDate,
LogoutDate = Temp1.LogoutDateTime,
LoginDuration = Temp1.LoginDuration,
RowVersion# = Temp1.RowVersion,
AgentID2 = Temp2.SkillTargetID,
LogonDate2 = Temp2.LogonDate,
LogoutDate2 = Temp2.LogoutDateTime,
RowVersion#2 = Temp2.RowVersion,
LogoutDuration = DateDiff(s,Temp1.LogoutDateTime,Temp2.LogonDate)
FROM
(SELECT
FullName = Person.LastName + ', ' + Person.FirstName,
SkillTargetID = Agent_Logout.SkillTargetID,
LogoutDateTime = Agent_Logout.LogoutDateTime,
LogonDate = DateAdd(s,-1 * Agent_Logout.LoginDuration,Agent_Logout.LogoutDateTime),
ROW_NUMBER() OVER(PARTITION BY Agent_Logout.SkillTargetID ORDER BY Agent_Logout.LogoutDateTime ASC) as RowVersion,
LoginDuration = Agent_Logout.LoginDuration
FROM Agent_Logout, Agent, Person
WHERE Agent_Logout.SkillTargetID = Agent.SkillTargetID and Agent.PersonID = Person.PersonID
) Temp1,
(SELECT
SkillTargetID = Agent_Logout.SkillTargetID,
LogoutDateTime = Agent_Logout.LogoutDateTime,
LogonDate = DateAdd(s,-1 * Agent_Logout.LoginDuration,Agent_Logout.LogoutDateTime),
ROW_NUMBER() OVER(PARTITION BY Agent_Logout.SkillTargetID ORDER BY Agent_Logout.LogoutDateTime ASC) as RowVersion
FROM Agent_Logout
WHERE Agent_Logout.SkillTargetID = Agent_Logout.SkillTargetID
) Temp2
WHERE Temp1.SkillTargetID = Temp2.SkillTargetID and Temp1.RowVersion = (Temp2.RowVersion - 1) AND
(Temp1.LogonDate >= :start_date
And Temp1.LogonDate <= :end_date) AND
Temp1.SkillTargetID IN (:agentid)
ORDER BY Temp1.SkillTargetID, Temp1.RowVersion