Question

In a previous question, you guys helped me grab data from a different row. The statement I am using works perfectly on the MS SQL Server Managment Studio. I can run the statement without any errors and I return the data I need. However, I need to run this data on our frontend program. When I try to run my statement on this program, it just hangs. I have a feeling that the "With As" part of this statement is causing problems. Is there anyway to rewrite this statement by putting this temporary table in a subquery?

WITH Temp1 AS (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) RowVersion,
LoginDuration = Agent_Logout.LoginDuration
FROM Agent_Logout)

SELECT
AgentID = Base.SkillTargetID,
LogonDate = Base.LogonDate,
BaseLogout = Base.LogoutDateTime,
BaseDuration = Base.LoginDuration,
NextLogon = Temp1.LogonDate,
LogoutDuration = DateDiff(s,Base.LogoutDateTime,Temp1.LogonDate)
FROM Temp1 Base
LEFT JOIN Temp1 ON Base.SkillTargetID = Temp1.SkillTargetID
AND Base.RowVersion = Temp1.RowVersion-1
Was it helpful?

Solution 2

Below is what I ended up with. This works with the Cisco front-end program we use.

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

OTHER TIPS

If you just want to materialise it you can do

;WITH Temp1 AS (
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) RowVersion,
LoginDuration = Agent_Logout.LoginDuration
FROM Agent_Logout)

SELECT * INTO #Temp1 FROM Temp1


SELECT
AgentID = Base.SkillTargetID,
LogonDate = Base.LogonDate,
BaseLogout = Base.LogoutDateTime,
BaseDuration = Base.LoginDuration,
NextLogon = #Temp1.LogonDate,
LogoutDuration = DateDiff(s,Base.LogoutDateTime,#Temp1.LogonDate)
FROM #Temp1 Base
LEFT JOIN #Temp1 ON Base.SkillTargetID = #Temp1.SkillTargetID
AND Base.RowVersion = #Temp1.RowVersion-1

It don't really understand what you mean by hanging when running on your frontend program though. Are you using the query exactly as written or are you parameterising it in some way?

Are you running it against the same data both times?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top